-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path15.3-sum.cpp
48 lines (44 loc) · 976 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
36
37
38
39
40
41
42
43
44
45
46
47
48
/*
* @lc app=leetcode id=15 lang=cpp
*
* [15] 3Sum
*/
// @lc code=start
class Solution
{
public:
vector<vector<int>> threeSum(vector<int> &nums)
{
vector<vector<int>> ans;
map<int, vector<int>> m;
set<vector<int>> s;
sort(nums.begin(), nums.end());
int n = nums.size();
for (int i = 0; i < n; i++)
{
int x = n - 1;
int j = i + 1;
while (j < x)
{
int now = nums[i] + nums[j] + nums[x];
if(now==0)
{
s.insert({nums[i], nums[j], nums[x]});
j++;
x--;
}
else if(now>0)
{
x--;
}
else{
j++;
}
}
}
for (auto k : s)
ans.push_back(k);
return ans;
}
};
// @lc code=end