-
Notifications
You must be signed in to change notification settings - Fork 110
/
15-3sum.cpp
30 lines (30 loc) · 1.07 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
#include <algorithm>
class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
int curr, left, right;
vector<vector<int>> result;
sort(nums.begin(), nums.end());
for (int curr = 0; curr < nums.size(); curr++) {
if (curr > 0 && nums[curr] == nums[curr - 1]) continue;
if (nums[curr] > 0) continue;
left = curr + 1;
right = nums.size() - 1;
while (left < right) {
int sum = nums[curr] + nums[left] + nums[right];
if (sum < 0) {
left++;
} else if (sum > 0) {
right--;
} else {
result.push_back({nums[curr], nums[left], nums[right]});
while (left + 1 < right && nums[left] == nums[left + 1]) left++;
while (right - 1 > left && nums[right] == nums[right - 1]) right--;
left++;
right--;
}
}
}
return result;
}
};