-
Notifications
You must be signed in to change notification settings - Fork 0
/
00015-3sum.cpp
47 lines (46 loc) · 1.46 KB
/
00015-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
/* medium :: two-pointers */
/*
Sort to avoid duplicates. For each nums[i],
do a two sum problem while avoiding duplicate
triplets.
- - - -
Time :: O(nlog(n))
Space :: O(1)
*/
class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums)
{
vector<vector<int>> ret;
sort(nums.begin(), nums.end());
int low, high, sum, lastLow, lastHigh;
int n = nums.size();
for (int i = 0; i < n; ++i)
{
if (nums[i] > 0) { break; }
if (i > 0 && nums[i] == nums[i - 1]) { continue; }
low = i + 1, high = n - 1;
// Two Sum ::
while (low < high)
{
sum = nums[i] + nums[low] + nums[high];
// Move pointers towards zero sum ::
if (sum > 0) { high--; }
else if (sum < 0) { low++ ; }
else
{
ret.push_back({nums[i], nums[low], nums[high]});
lastLow = nums[low], lastHigh = nums[high];
// Find next distinct low and high ::
while (low < high && nums[low] == lastLow) {
low++;
}
while (low < high && nums[high] == lastHigh) {
high--;
}
}
}
}
return ret;
}
};