给定一个含有 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. MySQL函数不能创建的解决方法(转)

    在使用MySQL数据库时,有时会遇到MySQL函数不能创建的情况.下面就教您一个解决MySQL函数不能创建问题的方法,供您借鉴参考. 出错信息大致类似: ERROR 1418 (HY000): Thi ...

  2. orcal数据库得连接必须用localhost,url中不要用127.0.0.1,不然无法连接

    orcal数据库得连接必须用localhost,url中不要用127.0.0.1,不然无法连接,

  3. PG degraded实验

    1. 创建一个文件,并把该文件作为对象放到集群中: [root@node1 ~]# echo "this is test! " >>test.txt [root@nod ...

  4. 十二:JAVA I/O

     输入模式                                                          输出模式 ⑴字节输入流:InputStream ⑵字节输出流:Output ...

  5. MySQL绿色版的安装步骤

    由于工作需要最近要开始研究MySQL了(看来学习都是逼出来的),本人对mysql没有研究,可以说一个小白. 下面就从安装开始吧,虽然网上关于这方面的东西很多,还是需要自己把操作过程写下来. 1.数据库 ...

  6. Centos6.5安装JDK8教程(一)

    [原] 转载请注明原文地址, 保持对知识基本尊重,谢谢!   Win7宿主系统 VmWareWorkstation 11应用下的 Centos6.5系统.   /******************* ...

  7. [#413c] Fountains

    http://codeforces.com/contest/799/problem/C 解题关键:树状数组取最大值,注意先搜索,后加入,此种情况可以取出最大值. 为什么可以取到最大值? 1.当分别用两 ...

  8. Struts2工作原理和执行流程图

    在struts2的应用中,从用户请求到服务器返回相应响应给用户端的过程中,包含了许多组件如:Controller.ActionProxy.ActionMapping.Configuration Man ...

  9. Ubuntu 使用 heirloom-mail 调用外部邮箱 SMTP 服务器发送邮件

    使用本地服务发邮件,经常被过滤掉而且占用资源,发送成功率不高.所以使用外部SMTP服务器发送邮件成为了需求. SMTP认证的目的是为了使用户避免受到垃圾邮件的侵扰,简单地说就是要求必须在提供了账户名和 ...

  10. vivado中如何调用chipscope或者impact

    vivado中并没有集成chipscope和impact,所以需要安装ISE,安装完ISE后进行以下操作: 1)     选择环境变量中的系统变量,新建以下变量 XILINX             ...