-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path15.cpp
More file actions
32 lines (32 loc) · 1.01 KB
/
Copy path15.cpp
File metadata and controls
32 lines (32 loc) · 1.01 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
class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
std::sort(nums.begin(),nums.end());
vector<vector<int>> res;
int len=nums.size();
if(len<3){return res;}
for(int i=0;i<len;i++){
int tar=nums[i];
if(tar>0){break;}
if(i>0&&tar==nums[i-1]){continue;}
int left=i+1,right=len-1;
int two=0-tar;
while(left<right){
int sum=nums[left]+nums[right];
if(sum>two){
right--;
}else if(sum<two){
left++;
}else{
res.push_back({tar,nums[left],nums[right]});
// has relationship with line 9
while(left<right&&nums[left]==nums[left+1]){left++;}
while(left<right&&nums[right]==nums[right-1]){right--;}
left++;
right--;
}
}
}
return res;
}
};