1.TwoSum

时间:2022-09-04 15:35:38

记录被LeetCode虐的日子

第一种方法:使用枚举

/**
* Note: The returned array must be malloced, assume caller calls free().
*/
int* twoSum(int* nums, int numsSize, int target)
{
int *result = (int*)malloc(2 * sizeof (int)); //申请内存
int i = 0;
int j = 0;
if(sizeof(nums) < 1) //输入参数有误判断
{
return NULL;
}
for(i = 0;i <= numsSize-1; i++)
{
for(j = 0;j <= numsSize-1;j++)
{
if((nums[i] + nums[j] == target) && (j != i) )
{
result[0] = i;
result[1] = j;
break;
}
}
}
return result;
}

注意:

1 代码开始,先检查边界值。如果为边界值,直接返回相应结果;如果是指针则检查是否为NULL.是数字的情况,要考虑特殊的数值,如零

2 提示错误:

load of null pointer of type 'const int'

在调用函数返回时,返回值如果是一个常量,则没问题。

返回值若为指针,则需注意会出现这个错误。如果返回的指针地址指向函数内的局部变量,在函数退出时,该变量的存储空间会被销毁,此时去访问该地址就会出现这个错误。

解决办法有以下三种:

1)返回的指针使用malloc分配空间

2)将该变量使用static修饰 static修饰的内部变量作用域不变 但是声明周期延长到程序结束 即该变量在函数退出后仍然存在

3)使用全局变量

建议使用malloc分配空间返回

3 指针数组初始化方法

指针数组中的每个元素都是一个指针,如下的array数组中的每个元素都是一个字符串指针,指向一个一维字符串数组。

char **array;
array = (char **)malloc(sizeof(char *) * 100) ; //array包含100个指针元素 为这100个指针变量分配空间
for(int i = 0; i < 100; i++)
array[i] = (char *)malloc(sizeof(char) * 50); // 为array中的每个指针变量进行初始化 上面的表达式只是为指针变量分配了空间 并没有为它们赋值 此语句为每个指针分配了长度为50个字符的空间 并将该空间的初始地址赋值给array中的指针

以下为第二次做这道题

  • 解法一

    解法一就是把数组中所有的值算出来,找到等于target的值,然后返回相应的位置即可。时间复杂度为O(N^2)

  /**
* Note: The returned array must be malloced, assume caller calls free().
*/
int* twoSum(int* nums, int numsSize, int target)
{
int *result = (int*)malloc(2 * sizeof (int)); //申请内存
int i = 0;
int j = 0;
if(sizeof(nums) < 1) //输入参数有误判断
{
return NULL;
}
for(i = 0;i <= numsSize-1; i++)
{
for(j = 0;j <= numsSize-1;j++)
{
if((nums[i] + nums[j] == target) && (j != i) )
{
result[0] = i;
result[1] = j;
break;
}
}
}
return result;
}
  • 解法二 使用Hash Map

    Hash Map来建立数字和坐标之间的映射关系,

    class Solution
{
public:
vector<int> twoSum(vector<int>& nums, int target)
{
unordered_map<int, int> datamap; //哈希表 用来建立数字和坐标之间的映射关系
vector<int> res;
for (int i = 0; i < nums.size(); i++)
{
datamap[nums[i]] = i;
} for (int i = 0; i < nums.size(); i++)
{
int t = target - nums[i]; //t 就是希望在哈希表中找到的元素
if (datamap.count(t) > 0)
{
//说明在哈希表中找到了t
res.push_back(i);
res.push_back(datamap[t]);
break;
} } return res;
}
};

上面代码存在一个BUG,当提交到leetcode时,testcase为{3,2,4},target = 6时可以测出这个BUG。修改后的代码为

    class Solution
{
public:
vector<int> twoSum(vector<int>& nums, int target)
{
unordered_map<int, int> datamap; //哈希表 用来建立数字和坐标之间的映射关系
vector<int> res;
for (int i = 0; i < nums.size(); i++)
{
datamap[nums[i]] = i;
} for (int i = 0; i < nums.size(); i++)
{
int t = target - nums[i]; //t 就是希望在哈希表中找到的元素
if (datamap.count(t) > 0 && datamap[t] != i)
{
//说明在哈希表中找到了t
res.push_back(i);
res.push_back(datamap[t]);
break;
} } return res;
}
};

1.TwoSum的更多相关文章

  1. JavaScript的two-sum问题解法

    一个很常见的问题,找出一个数组中和为给定值的两个数的下标.为了简单一般会注明解只有一个之类的. 最容易想到的方法是循环遍历,这里就不说了. 在JS中比较优雅的方式是利用JS的对象作为hash的方式: ...

  2. twoSum

    Given an array of integers, find two numbers such that they add up to a specific target number. The ...

  3. &lbrack;LeetCode&rsqb; TwoSum

    Given an array of integers, find two numbers such that they add up to a specific target number. The ...

  4. &lbrack;LeetCode&lowbar;1&rsqb; twoSum

    LeetCode: 1. twoSum 题目描述 Given an array of integers, return indices of the two numbers such that the ...

  5. &lbrack;Lintcode two-sum&rsqb;两数之和(python,双指针)

    题目链接:http://www.lintcode.com/zh-cn/problem/two-sum/ 给一个整数数组,找到两个数使得他们的和等于一个给定的数target. 备份一份,然后排序.搞两个 ...

  6. LeetCode初体验—twoSum

    今天注册了大名鼎鼎的LeetCode,做了一道最简单的算法题目: Given an array of integers, return indices of the two numbers such ...

  7. LeetCode——TwoSum

    题目: Given an array of integers, find two numbers such that they add up to a specific target number. ...

  8. LeetCode &num;1 TwoSum

    Description Given an array of integers, return indices of the two numbers such that they add up to a ...

  9. Leetcode 1——twosum

    Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...

  10. leetcode — two-sum

    package org.lep.leetcode.twosum; import java.util.Arrays; import java.util.HashMap; import java.util ...

随机推荐

  1. ajaxFileUpload插件

    关键词: $.ajaxFileUpLoad(); data status dataType 参考资料: http://www.cnblogs.com/kissdodog/archive/2012/12 ...

  2. Lae程序员小漫画(三),仅供一乐

    Lae软件开发,快乐程序员!

  3. MordenPHP阅读笔记(一)——先跑再说,跑累了再走

    ---恢复内容开始--- 后台一大堆半成品,或者是几乎不成的... 这本书不错,起码是别人推荐的,然后也是比较新的东西,学哪本不是学嘛,关键是得看. 今儿个网不好,科研所需的代码下不到,看书做笔记吧. ...

  4. Linux链接库二(动态库,静态库,库命名规则,建立个没有版本号的软连接文件)

    http://www.cppblog.com/wolf/articles/74928.html http://www.cppblog.com/wolf/articles/77828.html http ...

  5. 转:靠谱的代码和DRY

    http://www.cppblog.com/vczh/archive/2014/07/15/207658.html 靠谱的代码和DRY 上次有人来要求我写一篇文章谈谈什么代码才是好代码,是谁我已经忘 ...

  6. Appium 常用方法总结 &lpar;python 版&rpar;

    1.app后台运行 driver.background_app(5) 2.锁屏 driver.lock(5) 3.隐藏键盘 driver.hide_keyboard() 4.启动一个app或者在当前a ...

  7. BZOJ 3612&colon; &lbrack;Heoi2014&rsqb;平衡

    3612: [Heoi2014]平衡 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 283  Solved: 219[Submit][Status][ ...

  8. burpsuite两个变量的爱情故事

    抓包的时候在攻击类型处选择[Cluster bomb] 在payload type这里设置类型为[simple list] 第一个是账号 第二个是密码 分批加载即可

  9. BugPhobia开发篇章:Scurm Meeting-更新至0x03

    0x01 :目录与摘要 If you weeped for the missing sunset, you would miss all the shining stars 索引 提纲 整理与更新记录 ...

  10. php成绩排序

    $arr = ['12','12','23']; $arr = $arr; $arr1=$arr; rsort($arr1); $c=[]; foreach ( $arr as $k=>$v){ ...

相关文章