-
Notifications
You must be signed in to change notification settings - Fork 0
/
4Sum.cpp
47 lines (47 loc) · 1.23 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
38
39
40
41
42
43
44
45
46
47
class Solution {
public:
vector<vector<int> > fourSum(vector<int> &num, int target) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
vector<vector<int> > r;
if (num.size() < 4) {
return r;
}
sort(num.begin(), num.end());
for (int i = 0; i < num.size() - 3; i++) {
if (i > 0 && num[i] == num[i - 1]) {
continue;
}
for (int j = i + 1; j < num.size() - 2; j++) {
if (j > i + 1 && num[j] == num[j - 1]) {
continue;
}
int k = j + 1;
int l = num.size() - 1;
while (k < l) {
int sum = num[i] + num[j] + num[k] + num[l];
if (sum == target) {
vector<int> quadruplet;
quadruplet.push_back(num[i]);
quadruplet.push_back(num[j]);
quadruplet.push_back(num[k]);
quadruplet.push_back(num[l]);
r.push_back(quadruplet);
k++;
while (num[k - 1] == num[k]) {
k++;
}
l--;
while(num[l + 1] == num[l]) {
l--;
}
} else if (sum > target) {
l--;
} else {
k++;
}
}
}
}
}
};