-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path0015.cpp
More file actions
30 lines (27 loc) · 930 Bytes
/
Copy path0015.cpp
File metadata and controls
30 lines (27 loc) · 930 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> &num) {
sort(num.begin(), num.end());
vector<vector<int>> res;
for (int i = 0; i < num.size();) {
int target = -num[i], start = i + 1, end = num.size() - 1;
while (start < end) {
int sum = num[start] + num[end];
if (sum < target)
start++;
else if (sum > target)
end--;
else {
res.push_back({num[i], num[start], num[end]});
while (start < end && num[start] == res.back()[1])
start++;
while (start < end && num[end] == res.back()[2])
end--;
}
}
for (i++; i < num.size() && num[i] == num[i - 1]; i++)
;
}
return res;
}
};