-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path15_threeSum.cpp
More file actions
38 lines (38 loc) · 1.19 KB
/
15_threeSum.cpp
File metadata and controls
38 lines (38 loc) · 1.19 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
38
class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
vector<vector<int>> res;
if(nums.size()<3)
return res;
sort(nums.begin(),nums.end());
int begin,end,last;
for(int i = 0;nums[i]<=0&&i<nums.size()-2;i++){
if(nums[i] == last&&i>=1){
continue;
}
vector<int> tmp;
begin = i+1;
end = nums.size()-1;
while(begin<end){
if(nums[begin]+nums[end]+nums[i] == 0){
tmp.push_back(nums[i]);
tmp.push_back(nums[begin]);
tmp.push_back(nums[end]);
res.push_back(tmp);
tmp.clear();
begin++;
end--;
while(begin<end&&nums[begin] == nums[begin-1])
begin++;
while(end>begin&&nums[end]== nums[end+1])
end--;
}else if(nums[begin]+nums[end]+nums[i] < 0){
begin++;
}else
end--;
}
last = nums[i];
}
return res;
}
};