-
Notifications
You must be signed in to change notification settings - Fork 11
/
3 sum leetcode .cpp
37 lines (32 loc) · 994 Bytes
/
3 sum leetcode .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
class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
vector<vector<int>> ans;
if(nums.size() < 3) return ans;
sort(nums.begin(), nums.end());
for(int i = 0; i < nums.size()-2; ++i)
{
if(i != 0 && nums[i] == nums[i-1])
continue;
int a = i + 1, b = nums.size() -1, t;
while(a < b)
{
t = nums[i] + nums[a] + nums[b];
if(t > 0)
b--;
else if(t < 0)
a++;
else
{
ans.push_back({nums[i] , nums[a] , nums[b]});
a++; b--;
while(nums[a] == nums[a-1] && a < b)
a++;
while(nums[b] == nums[b+1] && a < b)
b--;
}
}
}
return ans;
}
};