-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path3Sum_15.cpp
65 lines (51 loc) · 1.8 KB
/
3Sum_15.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
60
61
62
63
64
65
/*
~ Author : leetcode.com/tridib_2003/
~ Problem : 15. 3Sum
~ Link : https://leetcode.com/problems/3sum/
*/
class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
vector<vector<int> > res;
int n = nums.size();
if (n < 3)
return res;
sort(nums.begin(), nums.end());
for (int i = 0; i < n - 2; ++i) {
int target = -nums[i];
if (target < 0)
break;
int l = i + 1, r = n - 1;
while (l < r) {
int l_val = nums[l], r_val = nums[r];
if (nums[l] + nums[r] == target) {
vector<int> triplet(3, 0);
triplet[0] = nums[i];
triplet[1] = nums[l];
triplet[2] = nums[r];
res.emplace_back(triplet);
// Skip current duplicates
while (l < r && nums[l] == triplet[1])
++l;
// Skip current duplicates
while (l < r && nums[r] == triplet[2])
--r;
}
else if (nums[l] + nums[r] < target) {
// Skip current duplicates
while (l < r && nums[l] == l_val)
++l;
}
else {
// Skip current duplicates
while (l < r && nums[r] == r_val)
--r;
}
}
// Skip current duplicates, keep in mind that for loop will also increment the value of i by 1
while (i + 1 < n && nums[i + 1] == nums[i])
++i;
}
return res;
}
};