-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathLC_167_TwoSumII.cpp
41 lines (34 loc) · 1.03 KB
/
LC_167_TwoSumII.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
/*
https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/
167. Two Sum II - Input Array Is Sorted
*/
class Solution {
public:
vector<int> twoSum(vector<int>& numbers, int target) {
int n = numbers.size();
int l=0;
int r=n-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--;
}
// unordered_map<int,int> um;
// vector<int> result;
// for(int i=0; i<n; i++){
// int rem_sum = target - numbers[i];
// if(um.find(rem_sum) != um.end())
// {
// result.push_back(i+1);
// result.push_back(um[rem_sum]+1);
// sort(result.begin(), result.end());
// return result;
// }
// um.insert({numbers[i],i});
// }
return {-1};
}
};