zoukankan      html  css  js  c++  java
  • LeetCode-4Sum

    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, abcd)
    • 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)
    复杂度:O(n^2)
    用两个for循环以及预处理过的数对
    class Solution{
        public:
    	vector<vector<int> > fourSum(vector<int> &num, int target) {
    
    		unordered_map<int,int> m;
    		//O(n) initialize
    		for(int i=0;i<num.size();i++){
    			if(m.find(num[i])==m.end()){
    				m[num[i]]=1;
    			}
    			else{
    				m[num[i]]++;
    			}
    		}
    		//O(n^2) initialize
    		unordered_map<int, set<vector<int> > > m2;
    		vector<int> p;
    		p.resize(2);
    		for(int i=0;i<num.size();i++){
    			for(int j=i+1;j<num.size();j++){
    				int b,s;
    				if(num[i]>num[j]){
    					b=num[i];
    					s=num[j];
    				}
    				else{
    					b=num[j];
    					s=num[i];
    				}
    				p[0]=s;
    				p[1]=b;
    				m2[b+s].insert(p);
    			}
    		}
    		vector<int> one;
    		one.resize(4);
    		set<vector<int> >::iterator it;
    
    		set<vector<int> >res;
    		vector<vector<int> > ret;
    		for( int i=0;i<num.size();i++)
    		{
    			for(int j=i+1;j<num.size();j++){
    				int tt=target-num[i]-num[j];
    				if(m2.find(tt)!=m2.end()){
    					set<vector<int> >& ss=m2[tt];
    					for(it=ss.begin();it!=ss.end();it++){
    						one[0]=num[i];
    						one[1]=num[j];
    						one[2]=(*it)[0];
    						one[3]=(*it)[1];
    						sort(one.begin(),one.end());
    						int current=one[0];
    						int count=1;
    						for(int i=1;i<4;i++){
    							if(one[i]==current){
    								count++;
    							}
    							else{
    								if(count>m[current])
    									continue;
    								else current=one[i];
    								count=1;
    							}
    						}
    						if(count>m[current])continue;
    						res.insert(one);
    					}
    				}
    			}
    		}
    		set<vector<int> >::iterator itt;
    		for(itt=res.begin();itt!=res.end();itt++){
    			ret.push_back(*itt);
    		}
    		return ret;
    	}
    };
    
  • 相关阅读:
    2020十一届蓝桥杯-七月省赛题解
    ES6 Generator与C#迭代器
    .NET Core微服务部分知识点
    js点击图片传给一个空的div内放大显示
    Jmeter 中 CSV 如何参数化测试数据并实现自动断言
    安装nvidia-docker2报错E: Unable to locate package nvidia-docker2
    微信小程序-点击事件传递参数
    2020牛客暑期多校训练营(第八场)I Interesting Computer Game K Kabaleo Lite
    Count the Colors
    Basic Gcd Problem
  • 原文地址:https://www.cnblogs.com/superzrx/p/3330731.html
Copyright © 2011-2022 走看看