-
Notifications
You must be signed in to change notification settings - Fork 0
/
15.txt
58 lines (29 loc) · 1.34 KB
/
15.txt
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
vector<vector<int>> re;
int n = nums.size();
if (n<3) return re;
sort(nums.begin(),nums.end());
for(int i=0; i<n&&nums[i]<=0;i++){
int target=-1*nums[i];
int left = i +1;
int right= n-1;
while(left<right){
int temp=nums[left]+nums[right];
if (temp< target) left++;
else if (temp> target) right--;
else{
vector<int> v={nums[i],nums[left],nums[right]};
re.push_back(v);
while(nums[left]==nums[left+1]) left++;
while(nums[right]==nums[right-1]) right--;
left++;
right--;
}
while (nums[i]==nums[i+1]) i++;
}
}
return re;
}
};