-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtwo_sum.cpp
More file actions
37 lines (33 loc) · 990 Bytes
/
two_sum.cpp
File metadata and controls
37 lines (33 loc) · 990 Bytes
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
#includealgorithm
class Solution {
public
static bool cmp(pairint, int &p1, pairint,int &p2){
return p1.secondp2.second;
};
vectorint twoSum(vectorint& nums, int target) {
vectorint v;
bool flag = false;
vectorpairint, int numbs;
for(int i = 0; inums.size();i++){
numbs.push_back(make_pair(i,nums[i]));
}
sort(numbs.begin(), numbs.end(),cmp);
for(int i = 0; inums.size();i++){
if(!flag){
for(int j = i+1; jnums.size();j++){
int candi = numbs[i].second + numbs[j].second;
if(target == candi){
flag = true;
v.push_back(numbs[i].first);
v.push_back(numbs[j].first);
break;
}
else if(candi target){
break;
}
}
}
}
return v;
}
};