-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.cpp
More file actions
48 lines (41 loc) · 976 Bytes
/
Copy pathsolution.cpp
File metadata and controls
48 lines (41 loc) · 976 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
44
45
46
47
48
#include <vector>
#include <set>
#include <algorithm>
using std::set;
using std::sort;
using std::vector;
class Solution
{
public:
vector<vector<int>> threeSum(vector<int> &nums)
{
set<vector<int>> triplets;
vector<vector<int>> res;
sort(nums.begin(), nums.end());
int n = nums.size();
for (int i = 0; i < n; i++)
{
int l = i + 1, r = n - 1;
while (l < r)
{
int currSum = nums[i] + nums[l] + nums[r];
if (currSum == 0)
{
triplets.insert({nums[i], nums[l], nums[r]});
l++;
}
else if (currSum < 0)
{
l++;
}
else
{
r--;
}
}
}
for (auto vec : triplets)
res.push_back(vec);
return res;
}
};