-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy path3sum.cpp
More file actions
30 lines (25 loc) · 772 Bytes
/
3sum.cpp
File metadata and controls
30 lines (25 loc) · 772 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>> ans;
int n = nums.size();
sort(nums.begin(), nums.end());
set<vector<int>> s;
for (int i = 0 ; i < nums.size(); i++) {
int l = i + 1;
int r = n - 1;
while (l < r) {
int sum = nums[i] + nums[l] + nums[r];
if (sum == 0) {
s.insert({nums[i], nums[l], nums[r]});
l++; r--;
}
else if (sum > 0) r--;
else l++;
}
}
for (auto it : s) ans.push_back(it);
// if(ans.size() == 0) return {};
return ans;
}
};