给定一个含有 n 个整数的数组 S,数列 S 中是否存在元素 a,b,c 和 d 使 a + b + c + d = target ?
请在数组中找出所有满足各元素相加等于特定值的不重复组合。
注意:解决方案集不能包含重复的四元组合。
例如,给定数组 S = [1, 0, -1, 0, -2, 2],并且给定 target = 0。
示例答案为:
[
  [-1,  0, 0, 1],
  [-2, -1, 1, 2],
  [-2,  0, 0, 2]
]
详见:https://leetcode.com/problems/4sum/description/

实现语言:Java

class Solution {
public List<List<Integer>> fourSum(int[] nums, int target) {
List result = new ArrayList<List<Integer>>();
if (nums == null || nums.length < 4){
return result;
} Arrays.sort(nums);
for (int i = 0; i < nums.length - 3; i++) {
if (i > 0 && nums[i] == nums[i-1]){
continue;
} for (int j = i + 1; j < nums.length - 2; j++) {
if (j > i + 1 && nums[j] == nums[j-1]){
continue;
} int left = j + 1;
int right = nums.length - 1;
while (left < right) {
int tmp = nums[i] + nums[j] + nums[left] + nums[right];
if (tmp == target) {
List l = new ArrayList<Integer>();
l.add(nums[i]);
l.add(nums[j]);
l.add(nums[left]);
l.add(nums[right]);
result.add(l);
while (left < right && nums[++left] == nums[left-1]);
while (left < right && nums[--right] == nums[right+1]);
} else if (tmp > target){
while (left < right && nums[--right] == nums[right+1]);
} else{
while (left < right && nums[++left] == nums[left-1]);
}
}
}
}
return result;
}
}

参考:https://www.jianshu.com/p/1ec86ec8feb6

018 4Sum 四个数的和的更多相关文章

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

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

  2. lintcode:四个数之和

    题目 四数之和 给一个包含n个数的整数数组S,在S中找到所有使得和为给定整数target的四元组(a, b, c, d). 样例 例如,对于给定的整数数组S=. 满足要求的四元组集合为: (-1, 0 ...

  3. js jq 手机号实现(344) 附带删除功能 jq 实现银行卡没四个数加一个空格 附带删除功能

    js 手机号实现(344)  下面有将正则验证去掉“-” 或“空格”  下一篇博客有单独的删除功能方法 <!DOCTYPE html> <head> <meta char ...

  4. 18 4Sum(寻找四个数之和为指定数的集合Medium)

    题目意思:给一个乱序数组,在里面寻找三个数之和为target的所有情况,这些情况不能重复,增序排列 思路:采用3Sum的做法 ps:有见一种用hash的,存任意两个元素的和,然后变成3sum问题,需要 ...

  5. 【LeetCode】018 4Sum

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

  6. [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 ...

  7. LeetCode 4Sum 4个数之和

    题意:这是继2sum和3sum之后的4sum,同理,也是找到所有4个元素序列,满足他们之和为target.以vector<vector<int>>来返回,也就是二维的,列长为4 ...

  8. [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 ...

  9. poj 1348 Computing (四个数的加减乘除四则运算)

    http://poj.org/problem?id=1348 Computing Time Limit: 1000MS   Memory Limit: 10000K Total Submissions ...

随机推荐

  1. flask+uswgi+nginx+python3.6的venv发布网站ubuntu14.04

    ---------------------------nginx--------------- sudo apt-get install nginx sudo apt-get remove nginx ...

  2. [转] 更新Flash CS6发布设置的目标播放器版本

    目前Aodbe发布的最新版的Flash CS6,都不支持将Flash Player 11作为目标播放器版本发布.这个问题很容易解决,但涉及到的东西却比较多,我在这里将一一讲解.首先来个Setp by ...

  3. 纯js+html+css实现模拟时钟

    前几天没事写的个模拟时钟,代码仅供小白参考,大神请自动绕过. <!DOCTYPE html> <html lang="en"> <head> & ...

  4. js数组中常用的几个API

    1.push:从末尾添加数据项. 2.pop:从末尾去除数据项. 3.shift:从开始去除数据项 4.splice: splice(m,n) m:指开始删除的索引位置  n:值删除几项 splice ...

  5. 阻止文件不被上传到iCloud

    转自:http://blog.csdn.net/a921800467b/article/details/38386787 http://www.cocoachina.com/bbs/read.php? ...

  6. httpd或Nginx负载均衡tomcat

    实验环境:CentOS7 #两台tomcat的基本配置如下: [root@webapps localhost]#setenforce 0 [root@webapps localhost]#iptabl ...

  7. oracle--视图(2)---

    Oracle---视图 视图是基于一个表或多个表或视图的逻辑表,本身不包含数据,通过它可以对表里面的数据进行查询和修改.视图基于的表称为基表,Oracle的数据库对象分为五种:表,视图,序列,索引和同 ...

  8. ASP.NET MVC 3:缓存功能的设计问题

    今天这一篇文章我来谈一谈在MVC 3项目中的缓存功能,以及针对缓存的一些设计上的考量,给大家参考参考. 为什么需要讨论缓存?缓存是一个中大型系统所必须考虑的问题.为了避免每次请求都去访问后台的资源(例 ...

  9. linux普通用户home目录锁定

  10. win10系统-javac不是内部或外部命令

    给笔记本装了一个ssd,上午装的系统,重新搞jdk,设置JAVA_HOME之后,cmd运行javac报 “javac不是内部或外部命令”各种懵逼,试了好几次才发现Path路径里面不能用%JAVA_HO ...