题目:

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. QString::arg()//用字符串变量参数依次替代字符串中最小数值

    QString i = "iTest";           // current file's number QString total = "totalTest&qu ...

  2. java面试数据类型

    1. Java的数据类型? 2. Java的封装类型? 3. 基本类型和封装类型的区别? 基本类型只能按值传递,而对应的封装类是按引用传递的. 基本类型是在堆栈上创建的,而所有的对象类型都是在堆上创建 ...

  3. iOS 11开发教程(十二)iOS11应用视图始祖——UIView

    iOS 11开发教程(十二)iOS11应用视图始祖——UIView 在Swift中,NSObject是所有类的根类.同样在UIKit框架(UIKit框架为iOS应用程序提供界面对象和控制器)中,也存在 ...

  4. vsftp 虚拟用户+MySQL认证独立家目录

    centos7 系统 安装包 yum -y install mariadb vsftpd openssl-devel  mysql-devel  pam-devel yum -y groupinsta ...

  5. 【CF398B】B. Painting The Wall(期望)

    B. Painting The Wall time limit per test 1 second memory limit per test 256 megabytes input standard ...

  6. 数据准备<4>:变量筛选-理论篇

    在上一篇文章<数据准备<3>:数据预处理>中,我们提到降维主要包括两种方式:基于特征选择的降维和基于维度转换的降维,其中基于特征选择的降维通俗的讲就是特征筛选或者变量筛选,是指 ...

  7. luoguP3185 [HNOI2007]分裂游戏 枚举 + 博弈论

    每个位置的瓶子中的每个石子是一个独立的游戏 只要计算出他们的\(sg\)值即可 至于方案数,反正不多\(n^3\)暴力枚举即可 反正怎么暴力都能过啊 复杂度\(O(Tn^3)\) #include & ...

  8. [BZOJ4864][BeiJing2017Wc]神秘物质(splay)

    首先merge就是先delete两次再insert,Max就是整个区间的最大值减最小值,Min就是区间中所有相邻两数差的最小值. Splay支持区间最大值,区间最小值,区间相邻差最小值即可. #inc ...

  9. 【取对数】【哈希】Petrozavodsk Winter Training Camp 2018 Day 1: Jagiellonian U Contest, Tuesday, January 30, 2018 Problem J. Bobby Tables

    题意:给你一个大整数X的素因子分解形式,每个因子不超过m.问你能否找到两个数n,k,k<=n<=m,使得C(n,k)=X. 不妨取对数,把乘法转换成加法.枚举n,然后去找最大的k(< ...

  10. hdu 4548 初始化+二分 *

    题意:小明对数的研究比较热爱,一谈到数,脑子里就涌现出好多数的问题,今天,小明想考考你对素数的认识.问题是这样的:一个十进制数,如果是素数,而且它的各位数字和也是素数,则称之为“美素数”,如29,本身 ...