-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsoln.cpp
28 lines (28 loc) · 871 Bytes
/
soln.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
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 l = i + 1;
int r = nums.size() - 1;
while (l < r)
{
int s = nums[i] + nums[l] + nums[r];
if (s == 0)
{
ans.push_back({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;
} else if (s < 0) ++l;
else --r;
}
}
return ans;
}
};