题目:

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. JQuery重定向

    window.location.href = "这里写页面的路径"; 如:window.location.href ="www.baidu.com";

  2. Spring boot集成Mybatis-Plus,通用Mapper

    Mybatis-Plus(简称MP)是一个 Mybatis 的增强工具,在 Mybatis 的基础上只做增强不做改变,为简化开发.提高效率而生.(摘自mybatis-plus官网)Mybatis虽然已 ...

  3. bzoj 4573: [Zjoi2016]大森林 lct splay

    http://www.lydsy.com/JudgeOnline/problem.php?id=4573 http://blog.csdn.net/lych_cys/article/details/5 ...

  4. 【枚举】AtCoder Regular Contest 095 C - Symmetric Grid

    题意:给你一个H*W的字符矩阵,一次操作可以任意将两行或者两列交换.问你是否能通过任意多次操作,使得其变为对称矩阵.对称的含义是:对于任何格子A(i,j),其都等于A(H-i+1,W-j+1). 显然 ...

  5. Alpha 冲刺报告6

    重感冒,重启中,停工一天

  6. bzoj 5055: 膜法师 -- 树状数组

    5055: 膜法师 Time Limit: 10 Sec  Memory Limit: 128 MB Description 在经历过1e9次大型战争后的宇宙中现在还剩下n个完美维度, 现在来自多元宇 ...

  7. ASP.NET 构建高性能网站 第3篇

    HTTP请求的优化 在一个网页的请求过程中,其实整个页面的html结构(就是页面的那些html骨架)请求的时间是很短的,一般是占整个页面的请求时间的10%-20%.在页面加载的其余的时间实际上就是在加 ...

  8. thrift 安装 make 失败 ar: .libs/ThriftTest_constants.o: No such file or directory

    $wget http://mirrors.cnnic.cn/apache/thrift/0.9.1/thrift-0.9.1.tar.gz $tar zxvf thrift-0.9.1.tar.gz ...

  9. select 语句的执行顺序

    select 语句的执行顺序 借用ItZik Ben-Gan.Lubor Kollar.Dejan Sarka所著的<Sql Server 2005 技术内幕:T-SQL查询>的一段话足以 ...

  10. POJ 2104 && POJ 2761 (静态区间第k大,主席树)

    查询区间第K大,而且没有修改. 使用划分树是可以做的. 作为主席树的入门题,感觉太神奇了,Orz /* *********************************************** ...