-
Notifications
You must be signed in to change notification settings - Fork 4
/
56_twoSum.cpp
41 lines (36 loc) · 1021 Bytes
/
56_twoSum.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/*
56 两数之和
给一个整数数组,找到两个数使得他们的和等于一个给定的数target。
你需要实现的函数twoSum需要返回这两个数的下标, 并且第一个下标小于第二个下标。
注意这里下标的范围是1到n,不是以0开头。
*/
class Solution {
public:
/*
* @param numbers : An array of Integer
* @param target : target = numbers[index1] + numbers[index2]
* @return : [index1+1, index2+1] (index1 < index2)
*/
vector<int> twoSum(vector<int> &nums, int target) {
// write your code here
if(nums.empty())
{
return vector<int>();
}//if
int len = nums.size();
vector<int> ret;
for(int i=0; i<len; ++i)
{
for(int j=i+1; j<len; ++j)
{
if(nums[i] + nums[j] == target)
{
ret.push_back(i+1);
ret.push_back(j+1);
return ret;
}//if
}//for
}//for
return vector<int>();
}
};