-
Notifications
You must be signed in to change notification settings - Fork 0
/
Three Sum
29 lines (28 loc) · 848 Bytes
/
Three Sum
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
class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
sort(nums.begin(), nums.end());
set<vector<int>>ans;
for(int i = 0;i<nums.size();i++){
if(i && nums[i] == nums[i-1]) continue;
int j = i+1, k = nums.size()-1;
while(j<k){
if(nums[i] + nums[j] + nums[k]< 0){
j++;
} else if(nums[i] + nums[j] + nums[k] > 0) {
k--;
} else {
ans.insert({nums[i],nums[j],nums[k]});
j++;
k--;
}
}
}
vector<vector<int>>ans1;
for(auto it:ans){
ans1.push_back(it);
}
return ans1;
}
};
// Question Link:- https://leetcode.com/problems/3sum/description/