-
Notifications
You must be signed in to change notification settings - Fork 1
/
4Sum_18.cpp
60 lines (45 loc) · 1.61 KB
/
4Sum_18.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
/*
~ Author : leetcode.com/tridib_2003/
~ Problem : 18. 4Sum
~ Link : https://leetcode.com/problems/4sum/
*/
class Solution {
public:
vector<vector<int>> fourSum(vector<int>& nums, int target) {
vector<vector<int> > res;
if (nums.empty())
return res;
int n = nums.size();
sort(nums.begin(), nums.end());
for (int i = 0; i < n; ++i) {
for (int j = i + 1; j < n; ++j) {
int rem_target = target - (nums[i] + nums[j]);
int l = j + 1, r = n - 1;
while (l < r) {
int two_sum = nums[l] + nums[r];
if (two_sum < rem_target)
++l;
else if (two_sum > rem_target)
--r;
else {
vector<int> quadruplet(4);
quadruplet[0] = nums[i];
quadruplet[1] = nums[j];
quadruplet[2] = nums[l];
quadruplet[3] = nums[r];
res.emplace_back(quadruplet);
while (l < r && nums[l] == quadruplet[2])
++l;
while (l < r && nums[r] == quadruplet[3])
--r;
}
}
while (j + 1 < n && nums[j + 1] == nums[j])
++j;
}
while (i + 1 < n && nums[i + 1] == nums[i])
++i;
}
return res;
}
};