-
Notifications
You must be signed in to change notification settings - Fork 12
/
solution.cpp
53 lines (49 loc) · 1.69 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
43
44
45
46
47
48
49
50
51
52
53
class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
vector<vector<int>> res;
// size of the array nums
int total = nums.size();
// sort the numbers
sort(nums.begin(), nums.end());
// fix the first number of the triplet
for (int firstNumIdx = 0; firstNumIdx < total; ++firstNumIdx) {
int firstNum = nums[firstNumIdx];
// find pairs with sum = -firstNum in the right
int start = firstNumIdx + 1;
int end = total - 1;
while (start < end) {
// consider the current pair sum
int current = nums[start] + nums[end];
if (current < -firstNum) {
// shift the start pointer to the right
++start;
} else if (current > -firstNum) {
// shift the end pointer to the left
--end;
} else {
// add to the result
res.push_back({firstNum, nums[start], nums[end]});
int oldStart = start;
int oldEnd = end;
// shift the start till it matches the old value
while (start < end && nums[start] == nums[oldStart]) {
++start;
}
// shift the end till it matches the old value
while (end > start && nums[end] == nums[oldEnd]) {
--end;
}
// the above two while loops ensure that both start/end
// get shifted atleast once
}
// avoid duplicates
while (firstNumIdx + 1 < total
&& nums[firstNumIdx + 1] == nums[firstNumIdx]) {
++firstNumIdx;
}
}
}
return res;
}
};