-
Notifications
You must be signed in to change notification settings - Fork 481
/
Copy path0167.cpp
40 lines (38 loc) · 845 Bytes
/
0167.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
#include <iostream>
#include <vector>
#include <cassert>
using namespace std;
static int x = []() {std::ios::sync_with_stdio(false); cin.tie(0); return 0; }();
class Solution
{
public:
vector<int> twoSum(vector<int>& numbers, int target)
{
int l = 0;
int r = numbers.size() - 1;
while (l < r)
{
if (numbers[l] + numbers[r] == target)
{
return { l + 1, r + 1 };
}
else if (numbers[l] + numbers[r] < target)
{
++l;
}
else
{
--r;
}
}
return {};
}
};
int main()
{
vector<int> numbers = {2, 7, 11, 15};
int target = 9;
for (auto& num : Solution().twoSum(numbers, target))
cout << num << endl;
return 0;
}