18.4Sum (Map)
Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target.
Note:
- Elements in a quadruplet (a,b,c,d) must be in non-descending order. (ie, a ≤ b ≤ c ≤ d)
- The solution set must not contain duplicate quadruplets.
For example, given array S = {1 0 -1 0 -2 2}, and target = 0. A solution set is: (-1, 0, 0, 1) (-2, -1, 1, 2) (-2, 0, 0, 2)
思路I: 用求3Sum的方法,外加一次for循环,时间复杂度O(n3)
class Solution { public: vector<vector<int>> fourSum(vector<int>& nums, int target) { int size = nums.size(); if(size < 3) return result; sort(nums.begin(), nums.end()); for(int i = 0; i < size-3; i++){ if(i > 0 && nums[i]==nums[i-1]) continue; //The solution set must not contain duplicate=>no duplicate in the same position for(int j = i+1; j < size-2; j++){ if(j> i+1 && nums[j]==nums[j-1]) continue; //The solution set must not contain duplicate=>no duplicate in the same position find(nums, j+1, size-1, target-nums[i]-nums[j], i, j); } } return result; } void find(vector<int>& nums, int start, int end, int target, int& index1, int& index2){ int sum; while(start<end){ sum = nums[start]+nums[end]; if(sum == target){ item.clear(); item.push_back(nums[index1]); item.push_back(nums[index2]); item.push_back(nums[start]); item.push_back(nums[end]); result.push_back(item); do{ //The solution set must not contain duplicate=>no duplicate in the same position start++; }while(start!= end && nums[start] == nums[start-1]); do{ //The solution set must not contain duplicate=>no duplicate in the same position end--; }while(end!=start && nums[end] == nums[end+1]); } else if(sum>target){ do{ //The solution set must not contain duplicate=>no duplicate in the same position end--; }while(end!=start && nums[end] == nums[end+1]); } else{//The solution set must not contain duplicate=>no duplicate in the same position do{ start++; }while(start!= end && nums[start] == nums[start-1]); } } } private: vector<vector<int>> result; vector<int> item; };
思路II:用hash table。O(N^2)把所有pair存入hash表,pair中两个元素的和就是hash值。那么接下来求4sum就变成了在所有的pair value中求 2sum,这个就成了线性算法了。所以整体上这个算法是O(N^2)+O(n) = O(N^2)。
class Solution { public: vector<vector<int>> fourSum(vector<int>& nums, int target) { int size = nums.size(); int a, b, c, d; vector<vector<int>> result; unordered_map<int,vector<pair<int,int> > > mp; unordered_map<int,int> cnt; //各个数的数量 if(size < 3) return result; sort(nums.begin(), nums.end()); for(int i = 0; i < size-1; i++){ if(i > 0 && nums[i]==nums[i-1]) continue; for(int j = i+1; j < size; j++){ if(j> i+1 && nums[j]==nums[j-1]) continue; mp[nums[i]+nums[j]].push_back(pair<int,int>{nums[i],nums[j]}); } } for(int i = 0; i < size; i++){ cnt[nums[i]]++; } for(unordered_map<int,vector<pair<int,int> > >::iterator it1=mp.begin();it1!=mp.end();it1++){//遍历map unordered_map<int,vector<pair<int,int> > >::iterator it2=mp.find(target - it1->first); //查找map if(it2==mp.end()) continue;// not found if(it1->first > it2->first) continue; //already checked,去重 for(int i = 0; i < it1->second.size(); i++){//访问map元素 for(int j = 0; j < it2->second.size(); j++){ a = it1->second[i].first; //访问pair元素 b = it1->second[i].second; c = it2->second[j].first; d = it2->second[j].second; if(max(a,b) > min(c,d)) continue; //四个数两两组合,有6种情况,这里只取两个最小的数在it1的情况,去重 cnt[a]--; cnt[b]--; cnt[c]--; cnt[d]--; if(cnt[a]<0||cnt[b]<0||cnt[c]<0||cnt[d]<0){ cnt[a]++; cnt[b]++; cnt[c]++; cnt[d]++; continue; } cnt[a]++; cnt[b]++; cnt[c]++; cnt[d]++; vector<int> tmp = {a,b,c,d}; sort(tmp.begin(),tmp.end()); result.push_back(tmp); } } } return result; } };