LeetCode-001 Two Sum
Given an array of integers, find two numbers such that they add up to a specific target number.
The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2...
分类:
其他好文 时间:
2014-05-15 04:34:50
阅读次数:
293
题意:给定一组数和另一个数,在这组数中找两个数,使它们的和等于给定的数
思路1: --> 错,因为题目要求返回下标。
1.排序
2.两个下标,一个指向头,一个指向尾
3.如果下标指向的两个元素相加大于给定的数,尾下标减一
如果小于,头下标加一
思路2: hash
1.用hash存储每个数的下标
2.数组,看hash[target-num[i]]是否存在
复杂度:时间O(n), 空间O(n)...
分类:
其他好文 时间:
2014-05-15 01:28:13
阅读次数:
297
[ 问题: ]
Given an array of integers, find two numbers such that they add up to a specific target number.
The function twoSum should return indices of the two numbers such that they add up to the ta...
分类:
其他好文 时间:
2014-05-15 00:04:39
阅读次数:
377
题目链接附上代码: 1 #include 2 #include 3 #include 4 using
namespace std; 5 6 class Solution { 7 public: 8 vector twoSum(vector
&numbers, int target) ...
分类:
其他好文 时间:
2014-05-08 17:50:11
阅读次数:
253
[Question]
Given an array of integers, find two numbers such that they add up to a specific target number.
The function twoSum should return indices of the two numbers such that they add up to t...
分类:
其他好文 时间:
2014-05-07 05:37:44
阅读次数:
278
这道题看似简单,其实很坑爹啊,动不动就是Time Limit Exceeded,
比如我下面这段TLE的代码,我感觉方法是对的,但是too
naive,算法复杂度到了O(N^2),自然就TLE了而且有一些小语法错误需要注意:for(runner=current+1;;)不能用>;一定所有的case都...
分类:
其他好文 时间:
2014-05-06 11:31:09
阅读次数:
405
原题地址:http://oj.leetcode.com/problems/two-sum/题意:找出数组numbers中的两个数,它们的和为给定的一个数target,并返回这两个数的索引,注意这里的索引不是数组下标,而是数组下标加1。比如numbers={2,7,11,17};
target=9。那...
分类:
编程语言 时间:
2014-04-30 21:45:43
阅读次数:
528
题目: Given an array of integers, find two numbers
such that they add up to a specific target number. The function twoSum should
return indices of the t...
分类:
其他好文 时间:
2014-04-30 03:20:07
阅读次数:
502