-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfour_sum.cpp
More file actions
132 lines (121 loc) · 3.57 KB
/
four_sum.cpp
File metadata and controls
132 lines (121 loc) · 3.57 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
* =====================================================================================
*
* Filename: four_sum.cpp
*
* Description: 4Sum: Given an array nums of n integers and an integer target, are
* there elements a, b, c, and d in nums such that a + b + c + d =
* target? Find all unique quadruplets in the array which gives the
* sum of target.
*
* Version: 1.0
* Created: 07/27/18 11:06:51
* Revision: none
* Compiler: gcc
*
* Author: Zhu Xianfeng (), xianfeng.zhu@gmail.com
* Organization:
*
* =====================================================================================
*/
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <vector>
class Solution
{
public:
std::vector<std::vector<int>> fourSum(std::vector<int>& nums, int target)
{
std::vector<std::vector<int>> sum_list;
if (nums.size() < 4)
{
return sum_list;
}
std::sort(nums.begin(), nums.end());
for (int i = 0; i < (nums.size() - 3); i++)
{
if ((i > 0) && (nums[i] == nums[i - 1]))
{
// Ignore duplicated
continue;
}
std::vector<std::vector<int>> three_list;
threeSum(std::vector<int>(nums.begin() + i + 1, nums.end()), target - nums[i], &three_list);
for (auto& num_list: three_list)
{
num_list.insert(num_list.begin(), nums[i]);
sum_list.push_back(num_list);
}
}
return sum_list;
}
private:
void threeSum(const std::vector<int>& nums, int target, std::vector<std::vector<int>>* sum_list)
{
for (int i = 0; i < (nums.size() - 2); i++)
{
if ((i > 0) && (nums[i] == nums[i - 1]))
{
// Ignore duplicated
continue;
}
int sum = 0;
int start = i + 1;
int end = nums.size() - 1;
while (start < end)
{
sum = nums[i] + nums[start] + nums[end];
if (sum == target)
{
sum_list->push_back(std::vector<int>({nums[i], nums[start], nums[end]}));
while ((start < (end - 1)) && (nums[start] == nums[start + 1]))
{
// Ignore duplicated
start++;
}
while ((end > (start + 1)) && (nums[end] == nums[end - 1]))
{
// Ignore duplicated
end--;
}
start++;
end--;
}
else if (sum < target)
{
start++;
}
else
{
end--;
}
}
}
}
};
int main(int argc, char* argv[])
{
// std::vector<int> nums = {1, 0, -1, 0, -2, 2};
// std::vector<int> nums = {-3, -2, -1, 0, 0, 1, 2, 3};
std::vector<int> nums = {-5, -4, -2, -2, -2, -1, 0, 0, 1};
int target = 0;
if (argc > 4)
{
nums.clear();
for (int i = 1; i < argc; i++)
{
nums.push_back(atoi(argv[i]));
}
}
auto sum_list = Solution().fourSum(nums, target);
for (const auto& num_list: sum_list)
{
for (auto n: num_list)
{
printf("%d ", n);
}
printf("\n");
}
return 0;
}