记录被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. [LeetCode] TwoSum

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

  4. [LeetCode_1] twoSum

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

  5. [Lintcode two-sum]两数之和(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 #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. Golang匿名函数

    概念所谓匿名函数,就是没有名字的函数匿名函数的两种使用方式一.在定义匿名函数的时候就可以直接使用(这种方式只使用一次) package main import ( "fmt" ) ...

  2. JavaScript数组实现图片轮播

    最终效果 注:图片来源于百度图片 文件结构: 代码: <!DOCTYPE html> <html> <head> <meta charset="UT ...

  3. linux交换区使用过多导致的性能问题

    近日,我们开发发现有一台配置相同的服务器跑的特别慢,相同数据量的情况下,其他服务器只要跑10分钟,这台服务器要跑50分钟,经确认,所有的应用层配置参数都相同.上去之后,发现该服务器swap使用比较多, ...

  4. 11:vue-cli脚手架

    1.1 vue-cli基本使用 官网: https://github.com/vuejs/vue-cli 1.简介 vue-cli 是一个vue脚手架,可以快速构造项目结构 vue-cli 本身集成了 ...

  5. 我仅使用到的dd if

    备份一个分区 分区 镜像名 读写块大小 dd if=/dev/sdb of=/diskone.img bs=512 注:可以为了提升I/O把bs设为较高的数值例:bs=1024k 挂载一个分区 mou ...

  6. android 文本框不获取焦点的两种方式

    当进入一个页面以后,我们不希望EditText获取焦点自动弹出软键盘,占据大半个屏幕. 方法一 让LinearLayout率先获取焦点,代码如下: <LinearLayout android:f ...

  7. CSS的再一次深入(更新中···)

    全面我们学了6个选择器,今天再来学习两个选择器,分别是通配符选择器和并集选择器: 1.通配符选择器: *{ } 表示body里所有的标签都被选中 2.并集选择器: 选中的标签之间用逗号隔开,表示这几个 ...

  8. 树形dp|无根树转有根树|2015年蓝桥杯生命之树

    2015年蓝桥杯第十题--生命之树(无根树dfs) ①暴力解法:枚举子集(选点) + dfs判断连通性(题目要求连通)满足上面两个条件下找出最大值权值和 ②dfs无根树转有根树,递归找最优 先学习无根 ...

  9. 【做题】agc002D - Stamp Rally——整体二分的技巧

    题意:给出一个无向连通图,有\(n\)个顶点,\(m\)条边.有\(q\)次询问,每次给出\(x,y,z\),最小化从\(x\)和\(y\)开始,总计访问\(z\)个顶点(一个顶点只计算一次),经过的 ...

  10. (转)Understanding Memory in Deep Learning Systems: The Neuroscience, Psychology and Technology Perspectives

    Understanding Memory in Deep Learning Systems: The Neuroscience, Psychology and Technology Perspecti ...