4Sum 数组中寻找四个数满足a+b+c+d=target

其实很简单的思想,4个数,其实就是 2+2,所以把数组的所有pairs和存入hash数组中,但要注意的一点是相同值不同位置的要区别对待。所以我存放的时候是sum值,与位置index pair。

然后根据a+b=target的方式求到对应的value组合即可。

public class Solution {
    public ArrayList<ArrayList<Integer>> fourSum(int[] num, int target) {
          // Note: The Solution object is instantiated only once and is reused by each test case.
          //如果是两重循环得到pair对,将每个pair对之后,放到hash数组中,value,pair1《A,B》 pair2<A,B>
          ArrayList<ArrayList<Integer>> results = new ArrayList<ArrayList<Integer>>();
          if (num != null && num.length > 3) {
              HashMap<Integer, ArrayList<ArrayList<Integer>>> pairs = new HashMap<Integer, ArrayList<ArrayList<Integer>>>(num.length*(num.length - 1));
              // sort num,有可能数组是重复的,如果建立pair怎么办,pair存num中的数字,但还是对num进行
              // 1, 2,2, 4(1,2) (1,4),(2, 4) 
              int tmpKey;
              Arrays.sort(num);
              ArrayList<Integer> tmpPairs = null;
              for (int i = 0; i < num.length; i++) {
                  for (int j = i + 1; j < num.length; j++) {
                      tmpKey = num[i] + num[j];
                      tmpPairs = new ArrayList<Integer>(2);
                      tmpPairs.add(i);
                      tmpPairs.add(j);
                      ArrayList<ArrayList<Integer>> tmp = (pairs.get(tmpKey) == null) ? new ArrayList<ArrayList<Integer>>() : pairs.get(tmpKey);
                      tmp.add(tmpPairs);
                      pairs.put(tmpKey, tmp);
                  }
              }
          
              Object[] keys = pairs.keySet().toArray();
              Arrays.sort(keys, 0, pairs.keySet().size());
              int big = keys.length - 1;
              int small = 0;
              int smallValue, bigValue;
              ArrayList<ArrayList<Integer>> smallList = new ArrayList<ArrayList<Integer>>();
              ArrayList<ArrayList<Integer>> bigList = new ArrayList<ArrayList<Integer>>();
              while (small <= big) {
                  smallValue = ((Integer)keys[small]).intValue();
                  bigValue = ((Integer)keys[big]).intValue();
                  if(smallValue + bigValue == target){
                      if (bigValue == smallValue) {
                          int size = pairs.get(smallValue).size();
                          while(size > 0) {
                              smallList.add(pairs.get(smallValue).get(--size));
                              if(size > 0) {bigList.add(pairs.get(smallValue).get(--size));}
                          }
                      }
                      else {
                              smallList = pairs.get(smallValue);
                              bigList = pairs.get(bigValue);
                      }
                      //考虑big和small相等
                      if(smallList != null & bigList != null) {
                          for(ArrayList<Integer> smallIndex: smallList) {
                              for(ArrayList<Integer> bigIndex: bigList) {
                                  if(smallIndex.get(0).intValue() != bigIndex.get(0).intValue() 
                                  && smallIndex.get(0).intValue() != bigIndex.get(1).intValue()
                                  && smallIndex.get(1).intValue() != bigIndex.get(0).intValue()
                                  && smallIndex.get(1).intValue() != bigIndex.get(1).intValue()) {
                                      ArrayList<Integer> tmp = new ArrayList<Integer>(4);
                                      tmp.add(num[smallIndex.get(0).intValue()]);
                                      tmp.add(num[smallIndex.get(1).intValue()]);
                                      tmp.add(num[bigIndex.get(0).intValue()]);
                                      tmp.add(num[bigIndex.get(1).intValue()]);
                                      Collections.sort(tmp);
                                      if (!results.contains(tmp)) {
                                          results.add(tmp);
                                      }
                                  }
                              }
                          }
                      }
                      small++;
                      big--;
                      smallList.clear();
                      bigList.clear();
                  }
                  else if(smallValue + bigValue < target) {
                      small++;
                  }
                  else {
                      big--;
                  }
              }
          }
          return results;
      }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值