【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 ...
随机推荐
- xorm使用pgsql的例子
测试表 /* Navicat Premium Data Transfer Source Server : localhost Source Server Type : PostgreSQL Sourc ...
- 将List<Map>中的datas转换为json格式写入文件
private static boolean writeToTextFileByJson(List<Map<String, Object>> datas, String tit ...
- LPC1768之中断
一外中断:只有特定的4个外中断引脚, 1 在特定的引脚上,引脚功能要选对应的外中断功能 2设定的触发条件(高低电平.上升/下降沿) 3 NVIC设定,特定的通道. 二GPIO中断: 1只有GPIO0和 ...
- Mac下安装JDK 6
https://support.apple.com/kb/DL1572?viewlocale=zh_CN&locale=en_US 下载 , 安装
- JS的Data类型格式化(转)
// 对Date的扩展,将 Date 转化为指定格式的String // 月(M).日(d).小时(h).分(m).秒(s).季度(q) 可以用 1-2 个占位符, // 年(y)可以用 1-4 个占 ...
- ibatis插入数据后返回自增长的主键
insert into testTable ( activity_id,activity_title values ( #{activityId,jdbcType=INTEGER}, #{activi ...
- Python应用01 原始Python服务器
作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 之前我的Python教程中有人留言,表示只学Python没有用,必须学会一个框架( ...
- 使用Topshelf 5步创建Windows 服务 z
使用Topshelf创建Windows 服务简要的介绍了创建Windows服务的另一种方法,老外的一篇文章Create a .NET Windows Service in 5 steps with T ...
- 中文unicode范围及unicode编解码
中文unicode范围 : [\u4e00-\u9fa5] 普通字符串可以用多种方式编码成Unicode字符串,具体要看你究竟选择了哪种编码:unicodestring = u"Hello ...
- Dapper.NET使用(转)
Dapper.NET使用 本文目录 Dapper.NET使用 1.为什么选择Dapper 2.以Dapper(4.0)为例. 2.1 在数据库中建立几张表. 2.2实体类. 3.使用方法 3.1 一 ...