-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path18.cpp
More file actions
30 lines (30 loc) · 969 Bytes
/
Copy path18.cpp
File metadata and controls
30 lines (30 loc) · 969 Bytes
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
class Solution {
public:
vector<vector<int>> fourSum(vector<int> &nums, int target) {
int size = nums.size();
if (size == 0)
return {};
set<vector<int>> vec_set;
sort(nums.begin(), nums.end());
for (int i = 0; i < size - 3; ++i) {
for (int j = i + 1; j < size - 2; ++j) {
int k = j + 1, l = size - 1, sum = nums[i] + nums[j];
while (k < l) {
if (sum + nums[k] + nums[l] > target)
--l;
else if (sum + nums[k] + nums[l] < target)
++k;
else {
vec_set.insert({nums[i], nums[j], nums[k], nums[l]});
--l;
++k;
}
}
}
}
vector<vector<int>> ret;
for (auto &s:vec_set)
ret.push_back(s);
return ret;
}
};