-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path15. 3Sum.cpp
More file actions
26 lines (23 loc) · 776 Bytes
/
15. 3Sum.cpp
File metadata and controls
26 lines (23 loc) · 776 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
class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
sort(nums.begin(), nums.end());
int N = nums.size();
set<vector<int>> ansSet;
for(int left = 0; left <= N-3; left++){
int mid = left+1, right = N-1;
while(mid < right){
if(nums[left] + nums[mid] + nums[right] == 0){
ansSet.insert({nums[left], nums[mid], nums[right]});
mid++; right--;
}else if(nums[left] + nums[mid] + nums[right] < 0){
mid++;
}else{
right--;
}
}
}
vector<vector<int>> ans(ansSet.begin(), ansSet.end());
return ans;
}
};