-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path4Sum.cpp
More file actions
42 lines (39 loc) · 1.26 KB
/
4Sum.cpp
File metadata and controls
42 lines (39 loc) · 1.26 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
36
37
38
39
40
41
42
#include <vector>
#include <algorithm>
#include <iostream>
#define FOR(i, a, b) for(int i=(a); i<(b); i++)
#define INCREASE(i, j, num) while(++i != (j) && (num[i])==(num[i-1]))
#define DECREASE(i, j, num) while(--i != (j) && (num[i])==(num[i+1]))
using namespace std;
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> > result;
int len = num.size();
if (len < 4) return result;
sort(num.begin(), num.end());
FOR(i, 0, len-3) {
FOR(j, i+1, len-2) {
int k = j+1, l = len-1;
while (k<l) {
int sum = num[i]+num[j]+num[k]+num[l];
if (sum < target) {
INCREASE(k, l, num);
} else if (sum > target) {
DECREASE(l, k, num);
} else {
int my_ints[4] = {num[i], num[j], num[k], num[l]};
std::vector<int> v(my_ints, my_ints+sizeof(my_ints)/sizeof(int));
if (find(result.begin(), result.end(), v) == result.end()) {
result.push_back(v);
}
INCREASE(k, l, num);
}
}
}
}
return result;
}
};