-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1.cpp
More file actions
34 lines (25 loc) · 852 Bytes
/
1.cpp
File metadata and controls
34 lines (25 loc) · 852 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
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
vector<pair<int,int>> newnums;
for(int i = 0; i < nums.size(); i++){
newnums.push_back({nums[i], i});
}
sort(newnums.begin(), newnums.end());
int l = 0; int r = newnums.size()-1;
vector<int> sol;
while(l < r){
while(l < r && newnums[l].first + newnums[r].first > target){
r--;
}
if(l == r) break;
if(newnums[l].first + newnums[r].first == target){
sol.push_back(newnums[l].second);
sol.push_back(newnums[r].second);
return sol;
}
l++;
}
return sol;
}
};