-
Notifications
You must be signed in to change notification settings - Fork 82
/
3sum.cpp
36 lines (36 loc) · 1.01 KB
/
3sum.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
class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
unordered_map<int,int>mp;
vector<vector<int>>ans;
sort(nums.begin(),nums.end());
for(int i=0;i<nums.size();i++)
{
int l=i+1,h=nums.size()-1;
int t=-nums[i];
while(l<h)
{
int sum=nums[l]+nums[h];
if(sum<t)
l++;
else if(sum>t)
h--;
else
{
vector<int>temp(3);
temp[0]=nums[i];
temp[1]=nums[l];
temp[2]=nums[h];
ans.push_back(temp);
while(l<h&&nums[l]==temp[1])
l++;
while(l<h&&nums[h]==temp[2])
h--;
while(i<nums.size()-1&&nums[i]==nums[i+1])
i++;
}
}
}
return ans;
}
};