-
Notifications
You must be signed in to change notification settings - Fork 0
/
4.fourSum.cpp
69 lines (57 loc) · 1.55 KB
/
4.fourSum.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
vector<vector<int>> fourSum(vector<int> &nums, int target) {
vector<vector<int>> res;
if (nums.size() < 4)
return res;
sort(nums.begin(), nums.end());
int i, j, k, m;
int len = nums.size();
for (i = 0; i < len - 3;) {
for (j = i + 1; j < len - 2;) {
k = j + 1;
m = len - 1;
while (k < m) {
int sum = nums[i] + nums[j] + nums[k] + nums[m];
if (sum == target) {
res.push_back({nums[i], nums[j], nums[k], nums[m]});
m--;
while (k < m && nums[m] == nums[m + 1]) {
m--;
}
k++;
while (k < m && nums[k] == nums[k - 1]) {
k++;
}
} else if (sum > target) {
m--;
} else {
k++;
}
}
j++;
while (j < len - 2 && nums[j] == nums[j - 1]) {
j++;
}
}
i++;
while (i < len - 3 && nums[i] == nums[i - 1]) {
i++;
}
}
return res;
}
int main() {
vector<int> nums = {-5, 5, 4, -3, 0, 0, 4, -2};
auto res = fourSum(nums, 4);
for (auto e : res) {
for (auto i : e) {
cout << i << " ";
}
cout << endl;
}
cout << "hello,word" << endl;
return 0;
}