题目:

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)

题解:
4 sum跟3 sum是一样的思路,只不过需要多考虑一个加数,这样时间复杂度变为O(n3)。 使用HashSet来解决重复问题的代码如下:
 1 public ArrayList<ArrayList<Integer>> fourSum(int[] num, int target) {
 2     HashSet<ArrayList<Integer>> hashSet = new HashSet<ArrayList<Integer>>();
 3     ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
 4     Arrays.sort(num);
 5     for (int i = 0; i <= num.length-4; i++) {
 6         for (int j = i + 1; j <= num.length-3; j++) {
 7             int low = j + 1;
 8             int high = num.length - 1;
 9  
             while (low < high) {
                 int sum = num[i] + num[j] + num[low] + num[high];
  
                 if (sum > target) {
                     high--;
                 } else if (sum < target) {
                     low++;
                 } else if (sum == target) {
                     ArrayList<Integer> temp = new ArrayList<Integer>();
                     temp.add(num[i]);
                     temp.add(num[j]);
                     temp.add(num[low]);
                     temp.add(num[high]);
  
                     if (!hashSet.contains(temp)) {
                         hashSet.add(temp);
                         result.add(temp);
                     }
  
                     low++;
                     high--;
                 }
             }
         }
     }
  
     return result;
 }
使用挪动指针的方法来解决重复的代码如下:
 1 public ArrayList<ArrayList<Integer>> fourSum(int[] num, int target) {
 2     HashSet<ArrayList<Integer>> hashSet = new HashSet<ArrayList<Integer>>();
 3     ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
 4     Arrays.sort(num);
 5     for (int i = 0; i <= num.length-4; i++) {
 6         if(i==0||num[i]!=num[i-1]){
 7             for (int j = i + 1; j <= num.length-3; j++) {
 8                 if(j==i+1||num[j]!=num[j-1]){
 9                     int low = j + 1;
                     int high = num.length - 1;
          
                     while (low < high) {
                         int sum = num[i] + num[j] + num[low] + num[high];
          
                         if (sum > target) {
                             high--;
                         } else if (sum < target) {
                             low++;
                         } else if (sum == target) {
                             ArrayList<Integer> temp = new ArrayList<Integer>();
                             temp.add(num[i]);
                             temp.add(num[j]);
                             temp.add(num[low]);
                             temp.add(num[high]);
          
                             if (!hashSet.contains(temp)) {
                                 hashSet.add(temp);
                                 result.add(temp);
                             }
          
                             low++;
                             high--;
                             
                             while(low<high&&num[low]==num[low-1])//remove dupicate
                                 low++;
                             while(low<high&&num[high]==num[high+1])//remove dupicate
                                 high--;
                         }
                     }
                 }
             }
         }
     }
  
     return result;
 }

4 Sum leetcode java的更多相关文章

  1. 【LeetCode】Path Sum ---------LeetCode java 小结

    Path Sum Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that addi ...

  2. Minimum Path Sum leetcode java

    题目: Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right w ...

  3. Binary Tree Maximum Path Sum leetcode java

    题目: Given a binary tree, find the maximum path sum. The path may start and end at any node in the tr ...

  4. Path Sum leetcode java

    题目: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up ...

  5. 3 Sum leetcode java

    题目: Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find al ...

  6. Two Sum Leetcode Java

    Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...

  7. Combination Sum leetcode java

    题目: Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C ...

  8. N-Queens II leetcode java

    题目: Follow up for N-Queens problem. Now, instead outputting board configurations, return the total n ...

  9. LeetCode算法题-Path Sum(Java实现)

    这是悦乐书的第169次更新,第171篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第28题(顺位题号是112).给定二叉树和整数sum,确定树是否具有根到叶路径,使得沿路 ...

随机推荐

  1. TCP 建立连接为什么要握 3 次手?

    上次已经说过,没有协议,不成方圆,计算机之间的通信更是依赖于协议.今天就重点分析一下 TCP 协议. 传输控制协议 TCP 是一种面向连接的.可靠的.基于字节流的传输层通信协议,由IETF的RFC 7 ...

  2. nginx + uswgi +django

    适合ubuntu 系统,不只是树莓派 安装必要软件 pt-get install build-essential psmisc apt-get install python-dev libxml2 l ...

  3. centos 7 部署k8s集群

    架构图: 前期准备 systemctl stop firewalldsystemctl disable firewalld yum -y install ntp systemctl start ntp ...

  4. Qt Quick快速入门之信号、槽

    信号和槽主要用于组件之间的通信,类似于.net和java中的委托. 使用QObject::connect方法将信号与槽关联起来,然后信号的发起者发出信号,接受者中的槽函数就会执行. 比如connect ...

  5. CF961E Tufurama 主席树

    对原问题进行转化 考虑对每个$i$,询问在$j \in [i + 1, a[i]]$中满足$a[j] \geqslant i$的个数 这样子可以做到不重不漏 个数满足差分的性质,使用主席树来维护即可 ...

  6. Template(Updating)

    1.Splay (Tyvj1728) #include <bits/stdc++.h> using namespace std; ,INF=<<; ],sz[MAXN],cnt ...

  7. Codeforces Round #461 (Div. 2)

    A - Cloning Toys /* 题目大意:给出两种机器,一种能将一种原件copy出额外一种原件和一个附件, 另一种可以把一种附件copy出额外两种附件,给你一个原件, 问能否恰好变出题目要求数 ...

  8. 【漏洞预警】方程式又一波大规模 0day 攻击泄漏,微软这次要血崩

    一大早起床是不是觉得阳光明媚岁月静好?然而网络空间刚刚诞生了一波核弹级爆炸!Shadow Brokers再次泄露出一份震惊世界的机密文档,其中包含了多个精美的 Windows 远程漏洞利用工具,可以覆 ...

  9. Codeforces Round #283 (Div. 2) B. Secret Combination 暴力水题

    B. Secret Combination time limit per test 2 seconds memory limit per test 256 megabytes input standa ...

  10. cocos2dx 字符串拼接

    ;i<;i++){ ]; sprintf(str,"%d",i); ]; strcpy(totalFilename, "game_loading") ; ...