forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
0015-3sum.cpp
59 lines (47 loc) · 1.45 KB
/
0015-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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*
Given int array, return all unique triplets that sum to 0
Ex. nums = [-1,0,1,2,-1,-4] -> [[-1,-1,2],[-1,0,1]]
Sort, for each i, have j & k go outside in, check for 0 sums
Time: O(n^2)
Space: O(n)
*/
class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
vector<vector<int>> result;
int n = nums.size();
if (n < 3) {
return result;
}
sort(nums.begin(), nums.end());
for (int i = 0; i < n - 2; i++) {
if (nums[i] > 0) {
break;
}
if (i > 0 && nums[i - 1] == nums[i]) {
continue;
}
int j = i + 1;
int k = n - 1;
while (j < k) {
int sum = nums[i] + nums[j] + nums[k];
if (sum < 0) {
j++;
} else if (sum > 0) {
k--;
} else {
result.push_back({nums[i], nums[j], nums[k]});
while (j < k && nums[j] == nums[j + 1]) {
j++;
}
j++;
while (j < k && nums[k - 1] == nums[k]) {
k--;
}
k--;
}
}
}
return result;
}
};