-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path167.cpp
44 lines (34 loc) · 1.08 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
class Solution {
public:
vector<int> twoSum(vector<int>& numbers, int target) {
int a[numbers.size() + 1];
for(int i = 1; i <= numbers.size(); i++){
a[i] = numbers[i - 1];
}
vector<int> ans;
for(int i = 1; i <= numbers.size(); i++){
int left = i + 1;
int right = numbers.size();
int meio = 0;
int procura = target - a[i];
bool encontrado = false;
while(left <= right){
meio = left + (right - left) / 2;
if(a[meio] == procura){
encontrado = true;
break;
}else if(a[meio] > procura){
right = meio - 1;
}else{
left = meio + 1;
}
}
if(encontrado){
ans.push_back(i);
ans.push_back(meio);
break;
}
}
return ans;
}
};