-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path15.cpp
More file actions
30 lines (30 loc) · 952 Bytes
/
Copy path15.cpp
File metadata and controls
30 lines (30 loc) · 952 Bytes
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
class Solution {
public:
vector<vector<int>> threeSum(vector<int> &nums) {
vector<vector<int>> ret;
auto size = nums.size();
std::sort(nums.begin(), nums.end());
for (int j = 0; j < size; ++j) {
int &&target = -nums[j];
int x = j + 1, y = size - 1;
while (x < y) {
if (nums[x] + nums[y] < target)
++x;
else if (nums[x] + nums[y] > target)
--y;
else {
ret.push_back({nums[j], nums[x], nums[y]});
do
++x;
while (x < y && nums[x] == nums[x - 1]);
do
--y;
while (x < y && nums[y] == nums[y + 1]);
}
}
while (j + 1 < size && nums[j + 1] == nums[j])
++j;
}
return ret;
}
};