-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path15_3_sum.cpp
35 lines (34 loc) · 989 Bytes
/
15_3_sum.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
auto init = []() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
return 0;
}();
class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
int n = nums.size();
sort(nums.begin(), nums.end());
vector<vector<int>> result;
for (int kx = n - 1; kx >= 0; --kx) {
if (kx < n - 1 && nums[kx] == nums[kx + 1])
continue;
int ix = 0, jx = kx - 1;
while (ix < jx) {
if (ix > 0 && nums[ix] == nums[ix - 1]) {
++ix;
continue;
}
if (nums[ix] + nums[jx] + nums[kx] == 0) {
result.push_back({nums[ix], nums[jx], nums[kx]});
++ix;
--jx;
} else if (nums[ix] + nums[jx] + nums[kx] < 0)
++ix;
else
--jx;
}
}
return result;
}
};