Repository navigation
Expand file tree
/
Copy path18four_sum.cpp
More file actions
100 lines (94 loc) · 2.99 KB
/
Copy path18four_sum.cpp
File metadata and controls
100 lines (94 loc) · 2.99 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
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std; //哈哈哈4545
/**18. 四数之和
* 给你一个由 n 个整数组成的数组 nums ,和一个目标值 target 。
* 请你找出并返回满足下述全部条件且不重复的四元组 [nums[a], nums[b], nums[c], nums[d]]
* (若两个四元组元素一一对应,则认为两个四元组重复):
*/
/**解答
* 和三个数之和一样,只不过要返回四个值,那么就需要去重复
* 首先长度小于三那直接返回
* 排序,然后从0-length-1选一个,从i+1-length-1开始第二个,首尾指针作为第三重循环
* 最重要的去重复,这步不容易想明白
*/
class Solution
{
public:
void addnum(vector<int> &nums, int n1, int n2, int n3, int n4)
{
auto it = nums.begin();
*(it++) = n1;
*(it++) = n2;
*(it++) = n3;
*it = n4;
}
vector<vector<int>> fourSum(vector<int> &nums, int target)
{
vector<vector<int>> ivec;
auto it = ivec.begin();
int length = nums.size();
if (length < 4)
return ivec;
int front = 0, rear = 0; //定义首尾指针
int nownum = 0;
sort(nums.begin(), nums.end()); //排序
for (int i = 0; i < length; ++i)
{
//去重
if (i > 0 && nums[i] == nums[i - 1])
{
continue;
}
for (int j = i + 1; j < length; ++j)
{
//去重
if (j > i + 1 && nums[j] == nums[j - 1])
{
continue;
}
front = j + 1;
rear = length - 1;
while (front < rear)
{
// nownum = nums[i] + nums[j] + nums[front] + nums[rear];溢出
if (nums[i] + nums[j] > target - nums[front] - nums[rear])
{
--rear;
}
else if (nums[i] + nums[j] < target - nums[front] - nums[rear])
{
++front;
}
else
{
ivec.push_back(vector<int>{nums[i], nums[j], nums[front], nums[rear]});
// 去重逻辑应该放在找到一个四元组之后
while (front < rear && nums[rear] == nums[rear - 1])
rear--;
while (front < rear && nums[front] == nums[front + 1])
front++;
++front;
--rear;
}
}
}
}
return ivec;
}
};
int main(int argc, char const *argv[])
{
vector<int> num{1, 0, -1, 0, -2, 2};
Solution sol;
for (auto vec : sol.fourSum(num, 0))
{
for (auto it : vec)
{
cout << it << ' ';
}
cout << endl;
}
return 0;
}