-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path18_4Sum.cpp
More file actions
74 lines (66 loc) · 2.29 KB
/
18_4Sum.cpp
File metadata and controls
74 lines (66 loc) · 2.29 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
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
69
70
71
72
73
74
// Given an array S of n integers, are there elements a, b, c, and d in S
// such that a + b + c + d = target? Find all unique quadruplets in the array
// which gives the sum of target.
// Note: The solution set must not contain duplicate quadruplets.
// For example, given array S = [1, 0, -1, 0, -2, 2], and target = 0.
// A solution set is:
// [
// [-1, 0, 0, 1],
// [-2, -1, 1, 2],
// [-2, 0, 0, 2]
// ]
// 解法:先排序,然后固定第一个元素(不同),接着用2sum的方法对撞型指针搜索
// 写的时候不要忘了排序!!
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class Solution {
public:
vector<vector<int>> fourSum(vector<int>& nums, int target) {
if (nums.size() < 4) return vector<vector<int> >();
int size = nums.size();
vector<vector<int> > ans;
sort(nums.begin(), nums.end());
for (int i = 0; i < size - 3; ++i) {
// de-duplicate & prune
if (i != 0 && nums[i] == nums[i - 1]) continue;
if (nums[i] + nums[i + 1] + nums[i + 2] + nums[i + 3] > target) break;
if (nums[i] + nums[size - 3] + nums[size - 2] + nums[size - 1] < target) continue;
int target1 = target - nums[i];
// 3 Sum
for (int j = i + 1; j < size - 2; ++j) {
// de-duplicate & prune
if (j != i + 1 && nums[j] == nums[j - 1]) continue;
if (nums[j] + nums[j + 1] + nums[j + 2] > target1) break;
if (nums[j] + nums[size - 2] + nums[size - 1] < target1) continue;
int target2 = target1 - nums[j];
// 2 Sum
int low = j + 1, high = size - 1;
while (low < high) {
if (nums[low] + nums[high] == target2) {
ans.push_back(vector<int>{nums[i], nums[j], nums[low], nums[high]});
while (low < high && nums[low + 1] == nums[low]) ++low;
while (low < high && nums[high - 1] == nums[high]) --high;
++low; --high;
} else if (nums[low] + nums[high] < target2) {
++low;
} else {
--high;
}
}
}
}
return ans;
}
};
int main() {
vector<int> nums = {1, 0, -1, 0, -2, 2};
Solution sol;
vector<vector<int> > ans = sol.fourSum(nums, 0);
for (vector<int> vec : ans) {
for (int n : vec) cout << n << " ";
cout << endl;
}
return 0;
}