-
Notifications
You must be signed in to change notification settings - Fork 0
/
018four_sum.cpp
40 lines (40 loc) · 1.49 KB
/
018four_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
36
37
38
39
40
class Solution {
public:
vector<vector<int>> fourSum(vector<int>& nums, int target) {
vector<vector<int>> result;
if(nums.size() < 4){
return result;
}
//sort the numbers;
sort(nums.begin(), nums.end());
//save a cache hashmap, key is sum of nums[a] and nums[b], value is pair<a, b>;
unordered_map<int, vector<pair<int, int>>> cache;
for(size_t a=0; a<nums.size(); ++a){
for(size_t b=a+1; b<nums.size(); ++b){
cache[nums[a] + nums[b]].push_back(make_pair(a, b));
}
}
for(int c=0; c<nums.size(); ++c){
for(size_t d=c+1; d<nums.size(); ++d){
const int key = target-nums[c]-nums[d];
//no value matches this key;
if(cache.find(key) == cache.end()){
continue;
}
//a vector of int pairs matches this key;
const auto& vec = cache[key];
for(size_t k=0; k<vec.size(); ++k){
if(c <= vec[k].second){
continue;
}
result.push_back({nums[vec[k].first], nums[vec[k].second], nums[c], nums[d]});
}
}
}
//sort and erase repetitive quadruplets;
sort(result.begin(), result.end());
auto end_unique = unique(result.begin(), result.end());
result.erase(end_unique, result.end());
return result;
}
};