-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path3sum.cpp
36 lines (27 loc) · 968 Bytes
/
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) {
vector<vector<int>>ans;
if( nums.size() < 3 )
return ans;
sort(nums.begin(),nums.end());
int l, r;
if(nums[0] > 0) return ans;
for(int i=0; i < nums.size(); i++) {
if( nums[i] > 0 ) break;
if( i > 0 && nums[i] == nums[i-1] ) continue;
l = i + 1, r = nums.size() - 1;
while( l < r ) {
if( nums[i] + nums[l] + nums[r] == 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( nums[i] + nums[l] + nums[r] > 0 ) r--;
else if( nums[i] + nums[l] + nums[r] < 0) l++;
}
}
return ans;
}
};