[leetcode]4Sum

4Sum

题意:给定一个array,找出四个数的组合相加等于target

解法:将每两个数的和放入形如key=integer,value=arraylist<integer>的hashtable,对target-数对的和进行判断,看是否存在这样的key,并且index与数对不冲突。由于枚举过程是枚举任意两个不同的数字,而枚举的判断过程是在一个arraylist中进行,这样的list的大小是O(n^2)级别的,所以单从这一点看,算法的复杂度至少是O(N^4)。但究竟有多少这样的数对,其实取决于答案的多少。

         我们可以通过对相同数字的出现次数限定不超过4次,来避免极端情况的存在。于是问题转化为,对于n个数,每个数相同的次数不超过4次,问题的答案数是否有比O(N^4)小的上届?

         这个问题还值得思考,我们先采用枚举其中3个数,那么target-三个数和一定只有一个数字,由于一个数字最多出现4次,于是至少我们的算法是O(N^3)的。

是否有更紧确上届正在思考中。

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Hashtable;
 
public class Solution135 {
         classTwoIndex{
                   inta;
                   intb;
                   TwoIndex(inta,int b){
                            this.a=a;
                            this.b=b;
                   }
                   booleanvalid(TwoIndex that){
                            if(that.a!=a&&that.a!=b&&that.b!=a&&that.b!=b){
                                     returntrue;
                            }else{
                                     returnfalse;
                            }
                   }
         }
   public ArrayList<ArrayList<Integer>> fourSum(int[] num, inttarget) {
             ArrayList<ArrayList<Integer>>ans=new ArrayList<ArrayList<Integer>>();
             HashSet<String> hashAns=newHashSet<String>();
             int n=num.length;
             Hashtable<Integer,ArrayList<TwoIndex>>hash=new Hashtable<Integer,ArrayList<TwoIndex>>();       
             for (int i=0;i<n;i++){
                       for (int j=i+1;j<n;j++){
                                intsum=num[i]+num[j];
                                if(!hash.containsKey(sum)){
                                         hash.put(sum,new ArrayList<TwoIndex>());
                                }
                                TwoIndex thisTwo=newTwoIndex(i,j);
                                hash.get(sum).add(thisTwo);
                                if (hash.containsKey(target-sum)){
                                         ArrayList<TwoIndex>temp=hash.get(target-sum);
                                         for (intk=0;k<temp.size();k++){
                                                   TwoIndexthat=temp.get(k);
                                                   if(thisTwo.valid(that)){
                                                            ArrayList<Integer>tempArr=new ArrayList<Integer>();
                                                            tempArr.add(num[thisTwo.a]);
                                                            tempArr.add(num[thisTwo.b]);
                                                            tempArr.add(num[that.a]);
                                                            tempArr.add(num[that.b]);
                                                            qsort(tempArr,0,3);
                                                            StringtempS=tempArr.get(0)+" "+tempArr.get(1)+""+tempArr.get(2)+" "+tempArr.get(3);
                                                            if(!hashAns.contains(tempS)){
                                                                     hashAns.add(tempS);
                                                                     ans.add(tempArr);
                                                            }
                                                   }
                                         }
                                }
                       }
             }
             return ans;          
    }
         privatevoid qsort(ArrayList<Integer> tempArr, int x, int y) {
                   inti=x;
                   intj=y;
                   intt=tempArr.get((i+j)/2);
                   do{
                            while(tempArr.get(i)<t){
                                     i++;
                            }
                            while(tempArr.get(j)>t){
                                     j--;
                            }
                            if(i<=j){
                                     inttemp=tempArr.get(i);
                                     tempArr.set(i,tempArr.get(j));
                                     tempArr.set(j,temp);
                                     i++;
                                     j--;
                            }
                   }while(i<=j);
                   if(j>x){
                            qsort(tempArr,x,j);
                   }
                   if(i<y){
                            qsort(tempArr,i,y);
                   }
         }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值