-
Notifications
You must be signed in to change notification settings - Fork 0
/
Two Sum.cpp
31 lines (30 loc) · 845 Bytes
/
Two Sum.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
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
int s = 0;
int e = nums.size()-1;
int mid = s + (e-s)/2;
vector<int> result;
while(s<=e){
if((nums[mid] + nums[mid+1]) == target){
result.push_back(mid);
result.push_back(mid + 1);
}
else if((nums[mid] + nums[mid-1]) == target){
result.push_back(mid - 1);
result.push_back(mid);
}
else if((nums[mid]+nums[mid+1]) > target){
e = mid - 1;
}
else if((nums[mid] + nums[mid-1]) < target){
s = mid + 1;
}
else{
continue;
}
mid = s + (e-s)/2;
}
return result;
}
};