-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfour_sum.cpp
35 lines (33 loc) · 1.25 KB
/
four_sum.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
class Solution {
public:
vector<vector<int>> fourSum(vector<int>& nums, int target) {
int s = nums.size();
set<vector<int> > ans;
std::sort(nums.begin(), nums.end());
for (int i = 0; i < s; ++i) {
for (int j = s - 1; j > 2; --j) {
int left = i + 1, right = j - 1;
while (left < right) {
int sum = nums[i] + nums[left] + nums[right] + nums[j];
if (sum < target) {
++left;
} else if (sum > target) {
--right;
} else {
vector<int> tmp = {nums[i], nums[left], nums[right], nums[j]};
ans.insert(tmp);
while (left < right && nums[left] == nums[left + 1]) {++left;}
while (left < right && nums[right] == nums[right - 1]) {--right;}
++left;
--right;
}
}
}
}
vector<vector<int> > t;
for (set<vector<int> >::iterator it = ans.begin(); it != ans.end(); ++it) {
t.push_back(*it);
}
return t;
}
};