【LeetCode】18. 4Sum
题目:
思路:这题和15题很像,外层再加一个循环稍作修改即可
- public class Solution {
- public List<List<Integer>> fourSum(int[] nums, int target) {
- List<List<Integer>> result=new ArrayList<List<Integer>>();
- int len=nums.length;
- if(len<4) return result;
- int sum=0;
- Arrays.sort(nums);
- for(int i=0;i<len-3;i++){
- if(4*nums[i]>target) break;
- if(i>0&&nums[i]==nums[i-1]) continue;
- for(int j=i+1;j<len-2;j++){
- if(j-i-1>0&&nums[j]==nums[j-1]) continue;
- int begin=j+1,end=len-1;
- while(begin<end){
- sum=nums[i]+nums[j]+nums[begin]+nums[end];
- if(sum==target){
- List<Integer> tem=new ArrayList<Integer>();
- tem.add(nums[i]);
- tem.add(nums[j]);
- tem.add(nums[begin]);
- tem.add(nums[end]);
- result.add(tem);
- begin++;end--;
- while(begin<end&&nums[begin]==nums[begin-1]) begin++;
- while(begin<end&&nums[end]==nums[end+1]) end--;
- }else if(sum<target){
- begin++;
- }else end--;
- }
- }
- }
- return result;
- }
- }
【LeetCode】18. 4Sum的更多相关文章
- 【LeetCode】18. 4Sum 四数之和
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:four sum, 4sum, 四数之和,题解,leet ...
- 【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 ...
- 【LeetCode】18、四数之和
题目等级:4Sum(Medium) 题目描述: Given an array nums of n integers and an integer target, are there elements ...
- 【一天一道LeetCode】#18. 4Sum
一天一道LeetCode (一)题目 Given an array S of n integers, are there elements a, b, c, and d in S such that ...
- 【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 = ...
- 【LeetCode】18.四数之和
题目描述 18. 四数之和 给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 t ...
- 【LeetCode】454. 4Sum II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 日期 题目地址:https://leetcod ...
- 【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 = ...
- 【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 ...
随机推荐
- 8张图带你深入理解Java
1.字符串的不变性 下图展示了如下的代码运行过程: String s = "abcd";s = s.concat("ef"); 备注:String refe ...
- bzoj2289: 【POJ Challenge】圆,圆,圆
Description 1tthinking随便地画了一些圆. ftiasch认为这些圆有交集(面积非零)的可能性不大.因为他实在画了太多圆,所以你被请来判断是否存在交集. Input 第1行,一个整 ...
- 什么是SQLCLR与使用
原帖地址:http://www.cnblogs.com/hsrzyn/archive/2013/05/28/1976555.html 什么是SQLCLR SQL CLR (SQL Common Lan ...
- python使用xlrd模块读写Excel文件的方法
本文实例讲述了python使用xlrd模块读写Excel文件的方法.分享给大家供大家参考.具体如下: 一.安装xlrd模块 到python官网下载http://pypi.python.org/pypi ...
- 2. hdfs
一.Hdfs的shell 所有hadoop的fs的shell均用uri路径作为参数 uri格式:schema://authority/path.hdfs的schema是hdfs.其中,schema和a ...
- 推荐一个css帮助手册的版本 同时提供chm和在线
版本保持更新 目录分类妥当 查阅很方便 就是习惯了jquery那种风格,略有不适应. 包括最新css3的内容 网址: http://css.doyoe.com/ chm下载地址: http://css ...
- 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 ...
- GL_Oracle Erp月结和年节流程讨论(概念)
2014-02-06 Created By BaoXinjian
- windows环境下 生成git公钥和私钥
windows环境下 生成公钥和私钥 上传代码到远程仓库的时候需要秘钥进行验证是否本人上传的.打开Git目录下的Git Bash 输入ssh-keygen,回车 可直接不输入路径,使用默认路径(c/U ...
- SQL语句的Select部分只写必要的列
如果Select部分包含不需要的列,这会强制DB2必须进入数据页来得到所请求的特定列,这就要求更多的I/O操作.另外,如果再对这个不需要的列进行排序,就需要创建和传递一个更大的排序文件,相应地会使排序 ...