-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfoursum.cpp
70 lines (60 loc) · 2.11 KB
/
foursum.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
64
65
66
67
68
69
/*Given an array nums of n integers, return an array of all the unique quadruplets [nums[a], nums[b], nums[c], nums[d]] such that:
0 <= a, b, c, d < n
a, b, c, and d are distinct.
nums[a] + nums[b] + nums[c] + nums[d] == target
You may return the answer in any order.
*/
class Solution {
public:
vector<vector<int>> fourSum(vector<int>& nums, int target) {
/* RUNTIME ERROR
int n = nums.size();
sort(nums.begin(), nums.end());
set<vector<int>> set;
vector<vector<int>> output;
for(int i=0; i<n-3; i++){
for(int j=i+1; j<n-2; j++){
int low = j+1, high = n-1;
while(low < high ){
if(nums[i] +nums[j]+ nums[low] + nums[high] < target)
low++;
else if(nums[i] +nums[j]+ nums[low] + nums[high] > target)
high--;
else{
set.insert({nums[i], nums[j],nums[low], nums[high]});
low++;
high--;
}
}
}}
for(auto it : set)
output.push_back(it);
return output; */
int n = nums.size();
sort(nums.begin(), nums.end());
set<vector<int>> set;
vector<vector<int>> output;
for(int i=0; i<n-3; i++){
for(int j=i+1; j<n-2; j++){
long long newTarget = (long long)target - (long long)nums[i] - (long long)nums[j];
int low = j+1, high = n-1;
while(low < high){
if(nums[low] + nums[high] < newTarget){
low++;
}
else if(nums[low] + nums[high] > newTarget){
high--;
}
else{
set.insert({nums[i], nums[j], nums[low], nums[high]});
low++; high--;
}
}
}
}
for(auto it : set){
output.push_back(it);
}
return output;
}
};