-
Notifications
You must be signed in to change notification settings - Fork 0
/
4sum.cpp
47 lines (45 loc) · 1.51 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) {
sort(num.begin(),num.end());
vector<vector<int> > result;
int len = num.size();
for (int i = 0; i < len -3; i++){
for (int j = len - 1; j > i + 2; j --){
int k = i + 1;
int z = j - 1;
while (k < z){
if (num[i] + num[j] + num[k] + num[z] == target){
vector<int> tem;
tem.push_back(num[i]);
tem.push_back(num[k]);
tem.push_back(num[z]);
tem.push_back(num[j]);
result.push_back(tem);
while (k + 1 < z && num[k] == num[k + 1]){
k ++;
}
while (z - 1 > k && num[z] == num[z - 1]){
z --;
}
k++;
z--;
}
else if ( num[i] + num[j] + num[k] + num[z] > target){
z --;
}
else {
k ++;
}
}
while (j - 1> i + 2 && num[j] == num[j - 1]){
j--;
}
}
while ( i + 1 < len -3 && num[i] == num[i + 1]){
i ++;
}
}
return result;
}
};