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