Given an array S of n integers, are there elements abc, 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: 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]
]

题目标签:Array
  这道题目给了我们一个nums array 和一个 target, 要我们找到所有的四个数字等于target 的可能性,并且不能重复。只要做过了threeSum,那么对于这一题,只要多加一层遍历就可以了。先来看一下threeSum,找三数之和,那一题里面要找 三数之和等于0,在这里,只要等于target就可以了,遍历nums array,对于每一个数字,利用twoSum(two pointers)方法来找到后面两个数字的所有可能性。那么来看这一题,这一题需要四个数字,a+b+c+d = target。那么我们遍历nums array, 对于每一个数字,就是确定了a, 接着第二层遍历,来确定b这个数字,当确定了b,利用twoSum来找到后面两个数字,c和d。 对于每一个a,要把所有b可能性都找了,对于每一个b,要把所有c和d的可能性都找了。简而言之,多加一层for loop就可以了。为了避免重复,依然要加 如果碰到遇到过的数字,就跳过的 设置。要注意的是,既然多了一个数字a, 那么在b里面 “如果遇到重复的数字就跳过” 这个设置里重复的数字不能包括a。
 
 

Java Solution:

Runtime beats 63.69%

完成日期:07/13/2017

关键词:Array

关键点:利用threeSum, 只要多加一层for loop,注意修改避免重复的条件

 
 
 public class Solution
{
public List<List<Integer>> fourSum(int[] nums, int target)
{
Arrays.sort(nums);
List<List<Integer>> res = new ArrayList<>(); for(int i=0; i<nums.length-3; i++)
{
if(i > 0 && nums[i] == nums[i-1]) // if previous num is same, skip this num
continue;
// find three sum
for(int j=i+1; j<nums.length-2; j++) // no need to find twoSum if rest array size is only 1 or 0
{
if(j-1 > i && nums[j] == nums[j-1])
continue;
// for each num, find the twoSum which equal -num in the rest array
int left = j + 1;
int right = nums.length-1; while(left < right)
{
int sum = nums[i] + nums[j] + nums[left] + nums[right];
if(sum == target) // find the two
{
res.add(Arrays.asList(nums[i], nums[j], nums[left], nums[right])); // ascending order
left++;
right--; while(left < right && nums[left] == nums[left-1]) // if next num is same as this, skip this
left++;
while(left < right && nums[right] == nums[right+1])
right--;
}
else if(sum > target) // meaning need smaller sum
right--;
else // nums[left] + nums[right] < sum, meaning need larger sum
left++;
} }
} return res;
}
}

参考资料:N/A

LeetCode 算法题目列表 - LeetCode Algorithms Questions List

LeetCode 18. 4Sum (四数之和)的更多相关文章

  1. [LeetCode] 18. 4Sum 四数之和

    Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = tar ...

  2. [leetcode]18. 4Sum四数之和

    Given an array nums of n integers and an integer target, are there elements a, b, c, and d in nums s ...

  3. 【LeetCode】18. 4Sum 四数之和

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:four sum, 4sum, 四数之和,题解,leet ...

  4. 【LeetCode 18】四数之和

    题目链接 [题解] 两重循环枚举[i..j]这个区间 同时规定必取nums[i]和nums[j] 那么现在的问题就变成在下标为[i..j]这个区间的数字里面找两个数字使他们的和为target-nums ...

  5. Leetcode(18)-四数之和

    给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 target 相等?找出所有满 ...

  6. 【LeetCode】18、四数之和

    题目等级:4Sum(Medium) 题目描述: Given an array nums of n integers and an integer target, are there elements ...

  7. [LeetCode] 4Sum 四数之和

    Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = tar ...

  8. [LeetCode] 454. 4Sum II 四数之和II

    Given four lists A, B, C, D of integer values, compute how many tuples (i, j, k, l) there are such t ...

  9. 【LeetCode】 454、四数之和 II

    题目等级:4Sum II(Medium) 题目描述: Given four lists A, B, C, D of integer values, compute how many tuples (i ...

  10. Java实现 LeetCode 18 四数之和

    18. 四数之和 给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 target ...

随机推荐

  1. 关于Linux的loop设备

    偶然发现/dev目录里有几个loop设备,一番搜索得知,这是一种伪设备(pseudo-device),它可以把一个文件连接为设备(就像Windows下用虚拟光驱挂载ISO文件). 遂做实验验证: 1. ...

  2. 图文详解在Windows server 2008 R2上安装SQL Server 2012集群

    1.准备: 4台服务器(1台AD.2台SQL服务器.1台iSCSI存储服务器) 9个IP(1个AD的IP.2个SQL服务器的IP.2个心跳IP.1个iSCSI存储服务器的IP.1个集群IP.1个DTC ...

  3. LCA问题第二弹

    LCA问题第二弹 上次用二分的方法给大家分享了对 LCA 问题的处理,各位应该还能回忆起来上次的方法是由子节点向根节点(自下而上)的处理,平时我们遇到的很多问题都是正向思维处理困难而逆向思维处理比较容 ...

  4. Linux安装pytorch的具体过程以及其中出现问题的解决办法

    1.安装Anaconda 安装步骤参考了官网的说明:https://docs.anaconda.com/anaconda/install/linux.html 具体步骤如下: 首先,在官网下载地址 h ...

  5. C++格式化输出的好东西

    s = FormatFloat("0.######", d); 最多保留6位s = FormatFloat("0.000000", d); 始终保留6位s = ...

  6. JSP入门 el表达式

    我们已经知道el是jsp-2.0规范的一部分,tomcat-5.x版本以上都已经能够支持jsp-2.0规范,但在更低版本的tomcat和webphere,weblogic中还是无法使用这一便捷方式. ...

  7. Day1 Python基础学习

    一.编程语言分类 1.简介 机器语言:站在计算机的角度,说计算机能听懂的语言,那就是直接用二进制编程,直接操作硬件 汇编语言:站在计算机的角度,简写的英文标识符取代二进制去编写程序,本质仍然是直接操作 ...

  8. 微信小程序的跨平台图表库开发

    写在前面 微信小程序出来已经有一段时间了,github上也有很多人开源了很多项目.但是由于微信平台的限制(底层Canvas能力调用为一系列JSBridge封装),图表的制作一直是个比较头疼的问题.当前 ...

  9. 应试记录2(没有转载标注,NOIP2016复赛过后自动删除)

    #include<stdio.h> #include<string.h> int main() { ]; memset(a, , sizeof(a)); ;i<=;i++ ...

  10. Java Socket通信以及可能出现的问题解决

    Java中基于TCP协议实现网络通信的两个类:客户端的Socket和服务器端的ServerSocket. Socket通信模型如图所示: 不管Socket通信的功能有多复杂,任何socket通信过程的 ...