给定一个含有 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. Poj 1061 青蛙的约会(扩展欧几里得解线性同余式)

    一.Description 两只青蛙在网上相识了,它们聊得很开心,于是觉得很有必要见一面.它们很高兴地发现它们住在同一条纬度线上,于是它们约定各自朝西跳,直到碰面为止.可是它们出发之前忘记了一件很重要 ...

  2. 上传下载,使用,commons-fileupload,commons-io来加载上传下载

    导入两个jarcommons-fileupload.jar,和commons-io.jar 在请求中创建核心类 // 1. 创建工厂对象 FileItemFactory factory = new D ...

  3. web攻击之四:DOS攻击

    DDOS是DOS攻击中的一种方法. DoS:是Denial of Service的简称,即拒绝服务,不是DOS操作系统,造成DoS的攻击行为被称为DoS攻击,其目的是使计算机或网络无法提供正常的服务. ...

  4. 写一个c程序辨别系统是16位or32位

    方法: 32位处理器就是一次只能处理32位,也就是4个字节的数据,虚拟地址空间的最大大小是4G,而64位处理一次就能处理64位,即8个字节的数据,最大虚拟地址空间的最大大小是16T.最明显的是指针大小 ...

  5. 面试题: mysql 数据库已看 sql安全性 索引 引擎 sql优化

    总结的一些MySQL数据库面试题 2016年06月16日 11:41:18 阅读数:4950 一.sql语句应该考虑哪些安全性? (1)防止sql注入,对特殊字符进行转义,过滤或者使用预编译的sql语 ...

  6. 《精通Spring4.X企业应用开发实战》读后感第六章(使用外部属性文件)

  7. sklearn解决过拟合的例子

    Learning curve 检视过拟合 sklearn.learning_curve 中的 learning curve 可以很直观的看出我们的 model 学习的进度, 对比发现有没有 overf ...

  8. Linux kernel分析前的准备

    分析工具 “欲善其事,先利其器”.Linux内核的代码量非常大,如果没有一个好的察看分析工具,那将是一件非常繁琐的事情. Vim+cscope cscope,如果你知道ctags,那么它是一个比cta ...

  9. 6.7 安装ant

    准备好安装包: 安装vim: 解压: tar -xzvf apahce-ant-1.10.1-bin.tar.gz 这里,我将apache-ant-1.10.1-bin.tar.gz复制并解压到了/h ...

  10. adb devices unauthorized解决方法

    有时候使用adb连接手机时,即使打开了usb调试,手机添加了信任,仍然出现unauthorized的提示 解决办法如下: 先上两张stack overflow上面的图片: 很多人可能看不懂.翻一下大概 ...