-
Notifications
You must be signed in to change notification settings - Fork 0
/
15_3Sum.cpp
28 lines (28 loc) · 904 Bytes
/
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
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 n = nums.size();
for(int i = 0; i < n - 2; i++) {
if(nums[i] > 0)
break;
if(i > 0 && nums[i] == nums[i-1])
continue;
for(int j = i+1, k = n-1; j < k; ) {
if(nums[i] + nums[j] + nums[k] == 0) {
res.push_back(vector<int>{nums[i],nums[j],nums[k]});
while(j < k && nums[j] == nums[++j]);
while(j < k && nums[k] == nums[--k]);
}
else if(nums[i] + nums[j] + nums[k] < 0)
j++;
else
k--;
}
}
return res;
}
};