-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathsolution167.cpp
More file actions
50 lines (45 loc) · 766 Bytes
/
solution167.cpp
File metadata and controls
50 lines (45 loc) · 766 Bytes
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
42
43
44
45
46
47
48
49
50
/**
* Two Sum II - Input array is sorted
*
* cpselvis([email protected])
* September 25th, 2016
*/
#include<iostream>
#include<vector>
using namespace std;
class Solution {
public:
vector<int> twoSum(vector<int>& numbers, int target) {
int i = 0, j = numbers.size() - 1;
vector<int> ret;
while (i < j)
{
int sum = numbers[i] + numbers[j];
if (sum == target)
{
ret.push_back(i + 1);
ret.push_back(j + 1);
break;
}
else if (sum < target)
{
i ++;
}
else
{
j --;
}
}
return ret;
}
};
int main(int argc, char **argv)
{
vector<int> nums({-3, 3, 4, 90});
Solution s;
vector<int> ret = s.twoSum(nums, 0);
for (auto i : ret)
{
cout << i << endl;
}
}