1. 原题链接

https://leetcode.com/problems/4sum/description/

2. 题目要求

给出整数数组S[n],在数组S中是否存在a,b,c,d四个整数,使得四个数之和等于目标整数target。请找出所有满足此条件的四个整数。

3. 解题思路

先对nums进行排序,然后采用两层for循环来确定前两个数字,最后在第二层for循环中确定后两个数字。

注意可能存在重复解!!

如下图所示,对Input先进行排序:[-4, -1, -1,0, 1,2],target = -1

存在两个“-1”,因此要考虑结果去重。

使用 if (i == 0 || (i > 0 && nums[i] != nums[i - 1])) 来对第一层for循环,即第一个数字去重。

如果对第二层for循环采用同样的方法去重,很可能导致丢失一个解,返回下图的错误结果。

[-4, -1, -1,0, 1,2],绿色表示第一层for循环遍历到的位置,红色表示第二层for循环开始的位置。如果使用 if (nums[j] != nums[j-1]) 来去重,就会跳过“-1”。

因此引入一个计数器count,来判断第二层for循环执行的次数。当count==1,即第二层for循环刚开始一次时,避免“-1”和“-1”的重复误判。

4. 代码实现

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; public class FourSum18 { public static void main(String[] args) {
int[] nums = {-1,-3,-2,2,3,-3,0,-4};
int target = 4;
List<List<Integer>> res = FourSum18.fourSum(nums, target);
for (List list : res) {
System.out.println(list);
}
} public static List<List<Integer>> fourSum(int[] nums, int target) {
ArrayList<List<Integer>> res = new ArrayList<List<Integer>>();
Arrays.sort(nums); // 对nums进行排序 for (int i = 0; i < nums.length - 3; i++) {
int sum1 = target - nums[i];
if (i == 0 || (i > 0 && nums[i] != nums[i - 1])) { // 去除遍历第一个数字时重复的结果
int count =0;
for (int j = i + 1; j < nums.length - 2; j++) {
/**
* 需要判断遍历第二个数字存在重复解的可能
* 要同时考虑第一次遍历的位置
* 用count计数第二个数遍历的次数
*/
count++; if (nums[j] != nums[j-1]|| count==1) { // 去除遍历第二个数字时重复的结果
int sum2 = sum1 - nums[j], l = j + 1, r = nums.length - 1;
while (l < r) {
if (nums[l] + nums[r] == sum2) {
res.add(Arrays.asList(nums[i], nums[j], nums[l], nums[r]));
while (l < r && nums[l] == nums[l + 1]) l++;
while (l < r && nums[r] == nums[r - 1]) r--;
l++;
r--;
} else if (sum2 < nums[l] + nums[r]) {
while (l < r && nums[r] == nums[r - 1]) r--;
r--; } else {
while (l < r && nums[l] == nums[l + 1]) l++;
l++;
}
}
}
} }
}
return res;
}
}

  

LeetCode:18. 4Sum(Medium)的更多相关文章

  1. LeetCode:11. ContainerWithWater(Medium)

    原题链接:https://leetcode.com/problems/container-with-most-water/description/ 题目要求:给定n个非负整数a1,a2,...,an  ...

  2. LeetCode:15. 3Sum(Medium)

    1. 原题链接 https://leetcode.com/problems/3sum/description/ 2. 题目要求 数组S = nums[n]包含n个整数,请问S中是否存在a,b,c三个整 ...

  3. LeetCode:46. Permutations(Medium)

    1. 原题链接 https://leetcode.com/problems/permutations/description/ 2. 题目要求 给定一个整型数组nums,数组中的数字互不相同,返回该数 ...

  4. LeetCode: 60. Permutation Sequence(Medium)

    1. 原题链接 https://leetcode.com/problems/permutation-sequence/description/ 2. 题目要求 给出整数 n和 k ,k代表从1到n的整 ...

  5. LeetCode: 61. Rotate List(Medium)

    1. 原题链接 https://leetcode.com/problems/rotate-list/description/ 2. 题目要求 给出一个链表的第一个结点head和正整数k,然后将从右侧开 ...

  6. LeetCode: 62. Unique Paths(Medium)

    1. 原题链接 https://leetcode.com/problems/unique-paths/description/ 2. 题目要求 给定一个m*n的棋盘,从左上角的格子开始移动,每次只能向 ...

  7. LeetCode: 56. Merge Intervals(Medium)

    1. 原题链接 https://leetcode.com/problems/merge-intervals/description/ 2. 题目要求 给定一个Interval对象集合,然后对重叠的区域 ...

  8. LeetCode: 55. Jump Game(Medium)

    1. 原题链接 https://leetcode.com/problems/jump-game/description/ 2. 题目要求 给定一个整型数组,数组中没有负数.从第一个元素开始,每个元素的 ...

  9. LeetCode: 54. Spiral Matrix(Medium)

    1. 原题链接 https://leetcode.com/problems/spiral-matrix/description/ 2. 题目要求 给定一个二维整型数组,返回其螺旋顺序列表,例如: 最后 ...

随机推荐

  1. 使用jvisualvm.exe工具查看java项目内存溢出(堆溢出)

    在查看内存溢出的时候,我们需要明白,堆溢出和持久代溢出,他们不一样,说到内存泄漏,我们就需要明白,内存中  年老代和新生代,和持久代,这3块的数据 自己的理解: new了一个对象,会进入到堆里面,先放 ...

  2. 创建maven项目后缺少jar包下载失败等问题

    transfer.......fail.........等问题 The container 'Maven Dependencies' references non existing library ' ...

  3. 最短路问题:迪杰斯特拉算法(Dijsktra)

    Dijkstra算法 1.定义概览 Dijkstra(迪杰斯特拉)算法是典型的单源最短路径算法,用于计算一个节点到其他所有节点的最短路径.主要特点是以起始点为中心向外层层扩展,直到扩展到终点为止.Di ...

  4. ATK-DataPortal 设计框架(一)

    无论是简单的还是复杂的框架,总需要一个开始的原点,ATK-DataPortal中包含了所有基础类的定义. 一.业务框架基础类 1.BusinessBase:所有业务类的根类,要使用ATK库的类,必需继 ...

  5. 小白袍 -- Chapter 1.4.1.1 URL编码的理论解读

    1.4.1.1  URL编码的理论解读 我们在做JavaWeb时避不过GET请求,GET请求和POST请求最大一点不同就在于参数,GET请求的参数会URL中,而POST请求的参数则会在HTTP Hea ...

  6. 2018 Wannafly summer camp Day8--区间权值

    区间权值 小Bo有\(n\)个正整数\(a_1\)--\(a_n\),以及一个权值序列\(w_1\)--\(w_n\),现在她定义\(f(l,r)=(\sum_{i=l}^r a_i^2) *w_{r ...

  7. 【TOJ 3305】Hero In Maze II

    描述 500年前,Jesse是我国最卓越的剑客.他英俊潇洒,而且机智过人^_^.突然有一天,Jesse心爱的公主被魔王困在了一个巨大的迷宫中.Jesse听说这个消息已经是两天以后了,他急忙赶到迷宫,开 ...

  8. 【PTA 天梯赛】L2-026. 小字辈(广搜+邻接表)

    本题给定一个庞大家族的家谱,要请你给出最小一辈的名单. 输入格式: 输入在第一行给出家族人口总数 N(不超过 100 000 的正整数) —— 简单起见,我们把家族成员从 1 到 N 编号.随后第二行 ...

  9. ABAP术语-IDOC

    IDOC 原文:http://www.cnblogs.com/qiangsheng/archive/2008/02/21/1075988.html Intermediate Document Inte ...

  10. Maven 运行启动时****找不到符号*com.xxx.user.java

    Maven 运行启动时****找不到符号*com.xxx.user.java maven项目更改后没有安装 (install) 重新安装解决问题!