-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathSolution.cpp
43 lines (39 loc) · 1.19 KB
/
Solution.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
#include <string>
#include <vector>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <tuple>
#include <deque>
#include <unordered_map>
using namespace std;
class Solution {
public:
// just like 3 sum
vector<vector<int>> threeSum(vector<int>& nums) {
sort(nums.begin(), nums.end());
vector<vector<int>> res;
if (nums.size() < 3) return res;
for (int i=0; i<nums.size()-2; i++) {
if (i>0 and nums[i] == nums[i-1]) continue;
int l = i + 1, r = nums.size() - 1;
// find all the twoSums and don't duplicate
while (l < r) {
if (nums[l] + nums[r] + nums[i] == 0) {
res.push_back({nums[i], nums[l], nums[r]});
// prevent dup
while (l + 1 < nums.size() and nums[l+1] == nums[l]) l ++ ;
while (r - 1 >= 0 and nums[r-1] == nums[r]) r -- ;
l ++ ; r -- ;
}
else if (nums[l] + nums[r] + nums[i] < 0) l ++;
else if (nums[l] + nums[r] + nums[i] > 0) r --;
}
}
return res;
}
};
int main() {
Solution s;
return 0;
}