forked from iphkwan/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
4_Sum.cc
53 lines (53 loc) · 1.79 KB
/
4_Sum.cc
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
class Solution {
public:
void add(vector<vector<int> > &ret, int a, int b, int c, int d) {
vector<int> tmp;
tmp.push_back(a);
tmp.push_back(b);
tmp.push_back(c);
tmp.push_back(d);
ret.push_back(tmp);
}
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> > ret;
if (num.size() < 4) {
return ret;
}
sort(num.begin(), num.end());
int left, right;
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;
}
left = j + 1;
right = num.size() - 1;
while (left < right) {
if (left != j + 1 && num[left] == num[left - 1]) {
left++;
continue;
}
if (right != num.size() - 1 && num[right] == num[right + 1]) {
right--;
continue;
}
int tmp = num[i] + num[j] + num[left] + num[right];
if (tmp == target) {
add(ret, num[i], num[j], num[left], num[right]);
left++;
} else if (tmp < target) {
left++;
} else {
right--;
}
}
}
}
return ret;
}
};