-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path4Sum
More file actions
107 lines (100 loc) · 3.55 KB
/
4Sum
File metadata and controls
107 lines (100 loc) · 3.55 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
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.
Example 1:
Input: nums = [1,0,-1,0,-2,2], target = 0
Output: [[-2,-1,1,2],[-2,0,0,2],[-1,0,0,1]]
Example 2:
Input: nums = [2,2,2,2,2], target = 8
Output: [[2,2,2,2]]
-----------------------------------------------------------------------------------------------------------------------------------------------------
//brute using 4 for loop
class Solution {
public:
vector<vector<int>> fourSum(vector<int>& nums, int target) {
set<vector<int>> st;
int n=nums.size();
for(int i=0;i<n-3;i++){
for(int j=i+1;j<n-2;j++){
for(int k=j+1;k<n-1;k++){
for(int l=k+1;l<n;l++){
long long sum=nums[i]+nums[j]+nums[k]+nums[l];
if(sum==target){
vector<int>temp={nums[i],nums[j],nums[k],nums[l]};
sort(temp.begin(),temp.end());
st.insert(temp);
}
}
}
}
}
vector<vector<int>>ans(st.begin(),st.end());
return ans;
}
};
------------------------------------------------------------------------------------------------------------
//better using hashmap
class Solution {
public:
vector<vector<int>> fourSum(vector<int>& nums, int target) {
set<vector<int>> st;
int n=nums.size();
for(int i=0;i<n;i++){
for(int j=i+1;j<n;j++){
set<long long>hash;
for(int k=j+1;k<n;k++){
long long sum=nums[i]+nums[j]+nums[k];
long long fourth=target-sum;
if(hash.find(fourth)!=hash.end()){
vector<int>temp={nums[i],nums[j],nums[k],(int)(fourth)};
sort(temp.begin(),temp.end());
st.insert(temp);
}
hash.insert(nums[k]);
}
}
}
vector<vector<int>>ans(st.begin(),st.end());
return ans;
}
};
----------------------------------------------------------------------------------------------------------
//optimal using 4 pointer
class Solution {
public:
vector<vector<int>> fourSum(vector<int>& nums, int target) {
vector<vector<int>>ans;
int n=nums.size();
sort(nums.begin(),nums.end());
for(int i=0;i<n;i++){
if(i>0 && nums[i]==nums[i-1]) continue;
for(int j=i+1;j<n;j++){
if(j>i+1 && nums[j]==nums[j-1])continue;
int k=j+1, l=n-1;
while(k<l){
long long sum=nums[i];
sum+=nums[j];
sum+=nums[k];
sum+=nums[l];
if(sum<target){
k++;
}
else if(sum>target){
l--;
}
else{
vector<int>temp={nums[i],nums[j],nums[k],nums[l]};
ans.push_back(temp);
k++;
l--;
while(k<l && nums[k]==nums[k-1]) k++;
while(k<l && nums[l]==nums[l+1]) l--;
}
}
}
}
return ans;
}
};