-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1.cpp
More file actions
37 lines (37 loc) · 1.03 KB
/
Copy path1.cpp
File metadata and controls
37 lines (37 loc) · 1.03 KB
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
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
vector<int>s;
vector<int>c;
c = nums;
sort(nums.begin(), nums.end());
int left = 0, right = nums.size()-1;
bool flag = false;
while(left < right && flag == false){
if((nums[left] + nums[right]) == target){
int i = 0;
for(i = 0; i < c.size(); ++i){
if(nums[left] == c[i]){
s.push_back(i);
break;
}
}
for(int j = 0; j < c.size(); ++j){
if(i == j) continue;
if(nums[right] == c[j]){
s.push_back(j); break;
}
}
flag = true;
break;
}
if(target < (nums[left] + nums[right])){
--right;
}
else{
++left;
}
}
return s;
}
};