-
-
Notifications
You must be signed in to change notification settings - Fork 300
/
167.cpp
54 lines (51 loc) · 1.4 KB
/
167.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
42
43
44
45
46
47
48
49
50
51
52
53
54
__________________________________________________________________________________________________
4ms
int optimization = []() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
return 0;
}();
class Solution {
public:
vector<int> twoSum(vector<int>& numbers, int target) {
vector<int> ans;
int l = 0, r = numbers.size() - 1;
while (l < r) {
int sum = numbers[l] + numbers[r];
if (sum == target) {
ans.push_back(l + 1);
ans.push_back(r + 1);
break;
} else if (sum < target) {
++l;
} else if (sum > target) {
--r;
}
}
return ans;
}
};
__________________________________________________________________________________________________
9296 kb
class Solution {
public:
vector<int> twoSum(vector<int>& numbers, int target) {
int l = 0, r = numbers.size() - 1;
while (l < r)
{
int sum = numbers[l] + numbers[r];
if(sum == target)
{
return {l + 1, r + 1};
}else if (sum < target)
{
++l;
}else
{
--r;
}
}
return {};
}
};
__________________________________________________________________________________________________