-
Notifications
You must be signed in to change notification settings - Fork 52
/
4Sum.cpp
37 lines (35 loc) · 1.2 KB
/
4Sum.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
class Solution {
public:
vector<vector<int> > fourSum(vector<int> &num, int target) {
vector<vector<int>>result;
int n = num.size();
sort(num.begin(), num.end());
for (int i = 0; i < n; i++) {
if (i > 0 && num[i-1] == num[i]) {
continue;
}
for (int j = i + 1; j < n; j++) {
if (j > (i + 1) && num[j-1] == num[j]) {
continue;
}
int l = j + 1, r = n - 1;
int partial_sum = num[i] + num[j];
while (l < r) {
int sum = partial_sum + num[l] + num[r];
if (sum < target) {
l++;
} else if (sum > target) {
r--;
} else {
result.push_back({num[i], num[j], num[l], num[r]});
while ((l + 1) < num.size() && num[l] == num[l+1]) l++;
l++;
while ((r - 1) >= 0 && num[r] == num[r - 1]) r--;
r--;
}
}
}
}
return result;
}
};