-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0018-4sum.cpp
More file actions
35 lines (35 loc) · 1.11 KB
/
0018-4sum.cpp
File metadata and controls
35 lines (35 loc) · 1.11 KB
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
class Solution {
public:
vector<vector<int>> fourSum(vector<int>& A, int target) {
int n = (int) A.size();
sort(A.begin(), A.end());
vector<vector<int>> ans;
vector<vector<int>> dups;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
long long t = (long long) target - A[i] - A[j];
int l = j + 1, r = n - 1;
while (l < r) {
if (A[l] + A[r] == t) {
vector<int> current = {A[l], A[r], A[i], A[j]};
sort(current.begin(), current.end());
dups.push_back(current);
l++;
r--;
} else if (A[l] + A[r] > t) {
r--;
} else {
l++;
}
}
}
}
sort(dups.begin(), dups.end());
for (auto &v: dups) {
if (ans.empty() || v != ans.back()) {
ans.push_back(v);
}
}
return ans;
}
};