题目:

思路:这题和15题很像,外层再加一个循环稍作修改即可

  1. public class Solution {
  2. public List<List<Integer>> fourSum(int[] nums, int target) {
  3. List<List<Integer>> result=new ArrayList<List<Integer>>();
  4. int len=nums.length;
  5. if(len<4) return result;
  6. int sum=0;
  7. Arrays.sort(nums);
  8. for(int i=0;i<len-3;i++){
  9. if(4*nums[i]>target) break;
  10. if(i>0&&nums[i]==nums[i-1]) continue;
  11. for(int j=i+1;j<len-2;j++){
  12. if(j-i-1>0&&nums[j]==nums[j-1]) continue;
  13. int begin=j+1,end=len-1;
  14. while(begin<end){
  15. sum=nums[i]+nums[j]+nums[begin]+nums[end];
  16. if(sum==target){
  17. List<Integer> tem=new ArrayList<Integer>();
  18. tem.add(nums[i]);
  19. tem.add(nums[j]);
  20. tem.add(nums[begin]);
  21. tem.add(nums[end]);
  22. result.add(tem);
  23. begin++;end--;
  24. while(begin<end&&nums[begin]==nums[begin-1]) begin++;
  25. while(begin<end&&nums[end]==nums[end+1]) end--;
  26. }else if(sum<target){
  27. begin++;
  28. }else end--;
  29. }
  30. }
  31.  
  32. }
  33. return result;
  34. }
  35. }

  

【LeetCode】18. 4Sum的更多相关文章

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

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

  2. 【LeetCode】18. 4Sum (2 solutions)

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

  3. 【LeetCode】18、四数之和

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

  4. 【一天一道LeetCode】#18. 4Sum

    一天一道LeetCode (一)题目 Given an array S of n integers, are there elements a, b, c, and d in S such that ...

  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】18.四数之和

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

  7. 【LeetCode】454. 4Sum II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 日期 题目地址:https://leetcod ...

  8. 【LeetCode】16. 4Sum

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

  9. 【LeetCode】454 4Sum II

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

随机推荐

  1. 8张图带你深入理解Java

    1.字符串的不变性 下图展示了如下的代码运行过程: String s = "abcd";s = s.concat("ef");   备注:String refe ...

  2. bzoj2289: 【POJ Challenge】圆,圆,圆

    Description 1tthinking随便地画了一些圆. ftiasch认为这些圆有交集(面积非零)的可能性不大.因为他实在画了太多圆,所以你被请来判断是否存在交集. Input 第1行,一个整 ...

  3. 什么是SQLCLR与使用

    原帖地址:http://www.cnblogs.com/hsrzyn/archive/2013/05/28/1976555.html 什么是SQLCLR SQL CLR (SQL Common Lan ...

  4. python使用xlrd模块读写Excel文件的方法

    本文实例讲述了python使用xlrd模块读写Excel文件的方法.分享给大家供大家参考.具体如下: 一.安装xlrd模块 到python官网下载http://pypi.python.org/pypi ...

  5. 2. hdfs

    一.Hdfs的shell 所有hadoop的fs的shell均用uri路径作为参数 uri格式:schema://authority/path.hdfs的schema是hdfs.其中,schema和a ...

  6. 推荐一个css帮助手册的版本 同时提供chm和在线

    版本保持更新 目录分类妥当 查阅很方便 就是习惯了jquery那种风格,略有不适应. 包括最新css3的内容 网址: http://css.doyoe.com/ chm下载地址: http://css ...

  7. 20160720-java高并发

    https://www.zhihu.com/search?type=content&q=tomcat+%E8%83%BD%E6%94%AF%E6%8C%81%E5%A4%9A%E5%B0%91 ...

  8. GL_Oracle Erp月结和年节流程讨论(概念)

    2014-02-06 Created By BaoXinjian

  9. windows环境下 生成git公钥和私钥

    windows环境下 生成公钥和私钥 上传代码到远程仓库的时候需要秘钥进行验证是否本人上传的.打开Git目录下的Git Bash 输入ssh-keygen,回车 可直接不输入路径,使用默认路径(c/U ...

  10. SQL语句的Select部分只写必要的列

    如果Select部分包含不需要的列,这会强制DB2必须进入数据页来得到所请求的特定列,这就要求更多的I/O操作.另外,如果再对这个不需要的列进行排序,就需要创建和传递一个更大的排序文件,相应地会使排序 ...