-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path15.cpp
More file actions
43 lines (40 loc) · 952 Bytes
/
15.cpp
File metadata and controls
43 lines (40 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
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
Solution()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
vector<vector<int>> threeSum(vector<int> &nums)
{
sort(nums.begin(), nums.end());
set<vector<int>> res;
for (int i = 0; i < nums.size() - 2; ++i)
{
int k = nums.size() - 1, j = i + 1;
while (j < k)
{
int sum = nums[i] + nums[j] + nums[k];
if (sum == 0)
{
res.insert({nums[i], nums[j], nums[k]});
j++;
k--;
}
else if (sum < 0)
{
j++;
}
else
{
k--;
}
}
}
return vector<vector<int>>(res.begin(), res.end());
}
};