-
Notifications
You must be signed in to change notification settings - Fork 1
/
3sum.cpp
31 lines (28 loc) · 902 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
// https://leetcode.com/problems/3sum/
class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
vector<vector<int>> v;
if(nums.size()<3) return v;
sort(nums.begin(), nums.end());
for(int i = 0; i<nums.size(); i++) cout<<nums[i]<<" ";
int i = 0;
int j = i + 1;
int k = nums.size() - 1;
while(i < nums.size() - 2){
while(j<k && (i==0 || nums[i]!=nums[i-1])){
if(nums[i] + nums[j] + nums[k] == 0){
v.push_back({nums[i],nums[j],nums[k]});
j++;
while(nums[j] == nums[j - 1] && j<k) j++;
}
else if(nums[i] + nums[j] + nums[k] > 0) k--;
else j++;
}
i++;
j = i + 1;
k = nums.size() - 1;
}
return v;
}
};