-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path4Sum.cpp
54 lines (50 loc) · 1.68 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
48
49
50
51
52
53
54
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
sort(num.begin(), num.end());
int len = num.size();
vector<vector<int> > result;
if (len < 4)
return result;
vector<int> tmp(4);
for (int i = 0; i != len - 1; i++){
for (int j = i+1; j != len; j++){
int tar = target - num[i] - num[j];
tmp[0] = num[i];
tmp[1] = num[j];
int p = 0, q = len - 1;
while(p < q){
if (p == i || p == j){
p++;
continue;
}
if (q == i || q == j){
q--;
continue;
}
int sum = num[p] + num[q];
if (sum == tar){
tmp[2] = num[p];
tmp[3] = num[q];
result.push_back(tmp);
p++;
q--;
continue;
}else if (sum > tar)
q--;
else if (sum < tar)
p++;
}
}
}
for (int i = 0; i != result.size(); i++){
sort(result[i].begin(), result[i].end());
}
sort(result.begin(), result.end());
vector<vector<int> >::iterator iter = unique(result.begin(), result.end());
result.erase(iter, result.end());
return result;
}
};