题目:

思路:这题和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的更多相关文章

  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. bzoj2012: [Ceoi2010]Pin

    Description 给出N(2<=N<=50000)个长度为4的字符串,问有且仅有D(1<=D<=4)处不相同的字符串有几对. Input 第1行: N,D 以下N行每行一 ...

  2. linux系统中rsync+inotify实现服务器之间文件实时同步

    最近需要对服务器上的文件实施动态备份,我又不想每次都手动来进行备份,在网上找了挺多资料,发现使用rsync就可以实现,如果想要实现实时同步,还可以使用rsync+inotify组合,本文就是以组合方式 ...

  3. Yii集成smarty说明

    1.       [在protected目录下建立文件夹vendor/smarty,把smarty的类包放入其中] 2.       [在extensions目录下边建立文件CSmarty.php] ...

  4. Objective C SEl 和@selector是怎么工作的||How do SEL and @selector work in iphone sdk?

    SEL is a type that represents a selector in Objective-C. The @selector() keyword returns a SEL that ...

  5. Eclipse - JDK内存配置- 环境配置

    ==================Eclipse环境配置=============================JDK : -Xms32m -Xmx800m backgroundColor: 85 ...

  6. java 编译中没有清除之前编译出来的文件。

    最近在写一个类时候使用了内部类,然后又将这个类改用普通类来实现.但在运行时访问局部变量时候出现了NullPointException异常,想来想去没有想明白,后来清除了一个之前编译出来的文件*.cla ...

  7. java finally中含return语句

    <java核心技术卷一>中提到过:当finally子句包含return 语句时(当然在设计原则上是不允许在finally块中抛出异常或者 执行return语句的,我不明白为何java的设计 ...

  8. nginx重写规则报nginx: [emerg] directive "rewrite" is not terminated by ";"

    对于下面的重写规则 rewrite ^/gongying/([\d]{8})_([\d]+).html$ /index.php?app=support&act=view&pts=$1& ...

  9. 转-封装网络请求库,统一处理通用异常 (基于volley网络请求库)

    http://blog.csdn.net/kroclin/article/details/40540761 一.前言 volley的发布让网络请求也变得十分便利,但是我们通常懒得很想用一两句代码实现一 ...

  10. java反射机制详解 及 Method.invoke解释

    JAVA反射机制 JAVA反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法:对于任意一个对象,都能够调用它的任意一个方法:这种动态获取的信息以及动态调用对象的方法的功能称为ja ...