-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path15.3sum.cpp
42 lines (35 loc) · 1.05 KB
/
15.3sum.cpp
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
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());
if(nums[0] > 0)
return res;
for(int k=0; k<nums.size(); k++)
{
int i=k+1, j=nums.size()-1;
if(k>0 && nums[k] == nums[k-1] )
continue;
while(i<j)
{
int temp = nums[i] + nums[j] + nums[k];
if(!temp)
{
res.push_back({nums[k], nums[i], nums[j]});
i++;
while(i<j && nums[i] == nums[i-1])
i++;
// while(i<j && nums[j] == nums[j+1])
// j--;
}
else if(temp > 0)
j--;
else
i++;
}
}
return res;
}
};