【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 ...
随机推荐
- bzoj2338 数矩形
给出N(N≤1500)个点,求选四个点作为顶点组成矩形的最大面积,保证有解. 对每两个点连边,按边长排序,枚举等长且中点相同的边作为对角线组成矩形,计算面积取最大值. 时间复杂度O(n2logn) # ...
- android学习笔记15——Galley
Gallery==>画廊视图 Gallery和Spinnery父类相同——AbsSpinner,表明Garrey和Spinner都是一个列表框. 两者之间的区别是:Spinner显示的是一个垂直 ...
- VS 使用Sql Server 数据库增删改查
/// <summary> /// 执行查询语句,返回DataSet /// </summary> /// <param name="SQLString&quo ...
- 反转(开关问题) POJ 3276
POJ 3276 题意:n头牛站成线,有朝前有朝后的的,然后每次可以选择大小为k的区间里的牛全部转向,会有一个最小操作m次使得它们全部面朝前方.问:求最小操作m,再此基础上求k. 题解:1.5000头 ...
- 在Visual Studio 2010/2012中 找不到创建WebService的项目模板
参考文章: http://blog.sina.com.cn/s/blog_6d545999010152wb.html 在 Visual Studio 2010 或者2012的新建 Web 应用程序或者 ...
- javascript世界一等公民—函数
简介 在很多传统语言(C/C++/Java/C#等)中,函数都是作为一个二等公民存在,你只能用语言的关键字声明一个函数然后调用它,如果需要把函数作为参数传给另一个函数,或是赋值给一个本地变量,又或是作 ...
- python (16) 如何在linux下安装lxml(pip安装,ubuntu下,centos下)
首先需要明白lxml包依赖其他包,必须先安装其他包然后再安装lxml 安装python-pip:方便安装python的第三方包 [root@******/]# wget https://bootstr ...
- Laravel 部署安装到虚拟主机的方法(折腾了一周,终于成功部署,原来是虚拟机不加载.env,谢谢莫回首http://lxl520.com/index.php/archives/88/!)
作者:莫回首链接:https://www.zhihu.com/question/35497879/answer/111241182来源:知乎著作权归作者所有,转载请联系作者获得授权. 序 lara ...
- Laravel 5 IDE Helper 安装
一.项目地址 https://github.com/barryvdh/laravel-ide-helper 二.安装方法 1.方法一:直接使用已经生成的文件 下载 https://gist.githu ...
- 两个Python web框架:Django & Tornado比较
就是说它作为 web 框架比 Django 简单,又支援异步 IO,且更不需要前端的 webserver ? 我已经混乱了, Tornado是 Nginx.Django.Node.js 的结合体?又或 ...