-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path4Sum.cpp
74 lines (71 loc) · 2.22 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
class Solution {
public:
vector<vector<int>> fourSum(vector<int>& nums, int target) {
vector<vector<int>> result;
if (nums.size() < 4)
return result;
sort(nums.begin(), nums.end());
coreKSum(nums, target, 4, vector<int>(), 0, result);
return result;
}
void coreKSum(vector<int>& nums, int target, int k, vector<int> numArray, int begin, vector<vector<int>>& result)
{
int sum = 0;
for (int i = 0; i < k; ++i)
{
sum += nums[begin + i];
}
if (sum > target)
return;
if (k == 2)
{
int end = nums.size() - 1;
sum = 0;
while (begin < end)
{
sum = nums[begin] + nums[end];
if (sum == target)
{
vector<int> tmp(numArray);
tmp.push_back(nums[begin]);
tmp.push_back(nums[end]);
result.push_back(tmp);
++begin;
while (begin < end && nums[begin] == nums[begin - 1]) ++begin;
}
else if (sum > target)
{
--end;
while (begin < end && nums[end] == nums[end + 1]) --end;
}
else // sum < target
{
++begin;
while (begin < end && nums[begin] == nums[begin - 1]) ++begin;
}
}
return;
}
// k > 2
numArray.push_back(0);
for(int i = begin; i <= nums.size() - k;)
{
sum = nums[i];
for (int j = 1; j < k; ++j)
{
sum += nums[nums.size() - j];
}
if (sum < target)
{
++i;
while (i <= nums.size() - k && nums[i] == nums[i - 1]) ++i;
continue;
}
numArray.pop_back();
numArray.push_back(nums[i]);
coreKSum(nums, target - nums[i], k - 1, numArray, i + 1, result);
++i;
while (i <= nums.size() - k && nums[i] == nums[i - 1]) ++i;
}
}
};