two sum
2014-07-23 23:00
162 查看
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. Please note that your returned answers (both index1 and
index2) are not zero-based.
You may assume that each input would have exactly one solution.
Input: numbers={2, 7, 11, 15}, target=9
Output: index1=1, index2=2
时间复杂度为O(n)
[/code]
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. Please note that your returned answers (both index1 and
index2) are not zero-based.
You may assume that each input would have exactly one solution.
Input: numbers={2, 7, 11, 15}, target=9
Output: index1=1, index2=2
时间复杂度为O(n)
class Solution {
public:
vector<int> twoSum(vector<int> &numbers, int target) {
int len = numbers.size();
assert(len >= 2);
vector<int> ret(2,0);
map<int, int> mapping;
vector<long long> mul(len,0);
for(int i=0; i<len; ++i)
{
mul[i] = (target - numbers[i]) * numbers[i];
if(mapping[mul[i]] >0)
{
if(numbers[mapping[mul[i]] -1] + numbers[i] == target)
{
ret[0] = mapping[mul[i]];
ret[1] = i+1;
return ret;
}
}
else
{
mapping[mul[i]] = i+1;
}
}
}
};
[/code]
相关文章推荐
- leetcode 46: Two Sum
- [LeetCode]Two Sum
- LeetCode | Two Sum
- Two Sum
- (LeetCode)Two Sum
- Two Sum
- Two Sum
- LeetCode Two Sum
- Two Sum
- Two Sum
- LeetCode——Two Sum
- [leetcode 1] Two Sum
- LeetCode 之 two Sum寻找两个相加之和为给定值的两个数
- [LeetCode]Two Sum
- Two Sum[leetcode]
- Leetcode-Two Sum
- [LeetCode]Two Sum 【Vector全局指针的使用】
- LeetCode[Hash Table]: Two Sum
- 和大神们学习每天一题(leetcode)-Two Sum
- 1 Two Sum