题目:

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. CI框架的事务开启、提交和回滚

    1.运行事务 $this->db->trans_start(); // 开启事务$this->db->query('一条SQL查询...');$this->db-> ...

  2. http远程调用原生get、post模板

    一.get方法 package lq.httpclient.method; import java.io.BufferedReader; import java.io.IOException; imp ...

  3. iOS 11开发教程(五)iOS11模拟器介绍二

    iOS 11开发教程(五)iOS11模拟器介绍二 3.iOS11模拟器中设置语言 对于不同国家的人来说,使用到的语言是不一样的.一般情况下iOS11模拟器默认使用的English(英语).对于英文不好 ...

  4. PHP 二维数组根据某个字段排序 复制代码 array_multisort

    //二维数组,按照里面的age从大到小降序,代码如下 <?php header('Content-Type:text/html;Charset=utf-8'); $arrUsers = arra ...

  5. codevs 5971 打击犯罪

    5971 打击犯罪 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 某个地区有n(n<=1000)个犯罪团伙,当地警方按照他们 ...

  6. [Agc011F] Train Service Planning

    [Agc011F] Train Service Planning 题目大意: 有n+1个车站,n条轨道,第i条轨道联通i-1和i车站,通过它要花a[i]时间,这条轨道有b[i]=1或2条车道,也就是说 ...

  7. bzoj 3931 最短路+最大流

    较水,但因为范围问题WA了两次.... /************************************************************** Problem: 3931 Us ...

  8. Codeforces Round #357 (Div. 2) A. A Good Contest 水题

    A. A Good Contest 题目连接: http://www.codeforces.com/contest/681/problem/A Description Codeforces user' ...

  9. CSS选择器复习

    通用选择器:* 选择到所有的元素 选择子元素:> 选择到元素的直接后代(第一级子元素) 相邻兄弟选择器:+ 选择到紧随目标元素后的第一个元素 普通兄弟选择器:~ 选择到紧随其后的所有兄弟元素 伪 ...

  10. Android 性能监控系列一(原理篇)

    欢迎关注微信公众号:BaronTalk,获取更多精彩好文! 一. 前言 性能问题是导致 App 用户流失的罪魁祸首之一,如果用户在使用我们 App 的时候遇到诸如页面卡顿.响应速度慢.发热严重.流量电 ...