-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3Sum.cpp
59 lines (53 loc) · 1.58 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
PROBLEM STATEMENT :- Given an integer array nums, return all the triplets [nums[i], nums[j], nums[k]] such that i != j, i != k, and j != k,
and nums[i] + nums[j] + nums[k] == 0.
Notice that the solution set must not contain duplicate triplets.
SOLUTION :-
class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums)
{
if(nums.size()<3)
{
return vector<vector<int>>{};
}
vector<vector<int>> rt;
sort(nums.begin(), nums.end());
for (int i=0; i<nums.size()-2; ++i)
{
if (nums[i]>0)
{
break;
}
//if a is pos, no combination can make it 0
if (i!=0 && nums[i-1]==nums[i])
{
continue;
}
//if repeat, skip
int l=i+1;
int r=nums.size()-1;
while (l<r)
{
if (nums[l]+nums[r]+nums[i]<0)
{
l++;
}
else if (nums[l]+nums[r]+nums[i]>0)
{
r--;
}
else
{
rt.push_back(vector<int>{nums[i],nums[l],nums[r]});
while (l<r && nums[l]==nums[l+1])
l++;
while (l<r && nums[r]==nums[r-1])
r--;
l++;
r--;
}
}
}
return rt;
}
};