forked from geemaple/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path18.4sum.cpp
63 lines (55 loc) · 1.67 KB
/
18.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
class Solution {
void twoSum(vector<int>& nums, int first, int second, int target, vector<vector<int>> &result)
{
int start = second + 1;
int end = nums.size() - 1;
while (start < end) {
int tmp = nums[first] + nums[second] + nums[start] + nums[end];
if (tmp == target)
{
result.push_back(vector<int>{nums[first], nums[second], nums[start], nums[end]});
while (start < end && nums[start + 1] == nums[start]) {
start += 1;
}
while (start < end && nums[end - 1] == nums[end]) {
end -= 1;
}
start += 1;
end -= 1;
}
else if (tmp > target)
{
end -= 1;
}
else
{
start += 1;
}
}
}
public:
vector<vector<int>> fourSum(vector<int>& nums, int target) {
if (nums.size() < 4)
{
vector<vector<int>>();
}
sort(nums.begin(), nums.end());
vector<vector<int>> res;
for (auto i = 0; i < nums.size(); ++i)
{
if (i > 0 && nums[i] == nums[i - 1])
{
continue;
}
for (auto j = i + 1; j < nums.size(); ++j)
{
if (j > i + 1 && nums[j] == nums[j - 1])
{
continue;
}
twoSum(nums, i, j, target, res);
}
}
return res;
}
};