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