public class S018 {
//借鉴S015,速度有些慢
public List<List<Integer>> fourSum(int[] nums, int target) {
Arrays.sort(nums);
List<List<Integer>> result = new ArrayList<List<Integer>>();
for(int i =0;i<nums.length-3;i++){
if(i>0&&nums[i]==nums[i-1]){
continue;//重复的直接跳过
}
for(int j = i+1;j<nums.length-2;j++){
if(j>i+1&&nums[j]==nums[j-1]){
continue;//重复的直接跳过
}
int left = j+1;//从i+1开始也是防止重复的办法
int right = nums.length-1;
while(left<right){
if(nums[left]+nums[right]+nums[i]+nums[j] == target){
List<Integer> temp = new ArrayList<Integer>();//必须每次新建
temp.add(nums[i]);
temp.add(nums[left]);
temp.add(nums[right]);
temp.add(nums[j]);
Collections.sort(temp);
result.add(temp);
//特别注意下面两个while循环
left++;
right--;//防止重复
while(left<right&&nums[left]==nums[left-1]){
left++;//防止重复
}
while(left<right&&nums[right]==nums[right+1]){
right--;//防止重复
}
//这两个条件特别重要,思考一下为何分别是left++和right--;
}else if(nums[left]+nums[right]+nums[i]+nums[j]<target){
left++;
}else{
right--;
}
}
}
}
return result;
}
}

Leetcode018 4Sum的更多相关文章

  1. [LeetCode] 4Sum II 四数之和之二

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

  2. [LeetCode] 4Sum 四数之和

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

  3. LeetCode:3Sum, 3Sum Closest, 4Sum

    3Sum Closest Given an array S of n integers, find three integers in S such that the sum is closest t ...

  4. 2016/10/28 很久没更了 leetcode解题 3sum问题进阶版4sum

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

  5. No.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. 6.3Sum && 4Sum [ && K sum ] && 3Sum Closest

    3Sum Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find a ...

  7. 3Sum & 4Sum

    3 Sum Given an array S of n integers, are there elements a, b, c in Ssuch that a + b + c = 0? Find a ...

  8. 【leetcode】4Sum

    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. 2sum、3sum、4sum以及任意连续的数的和为sum、任意连续或者不连续的数的和为sum

    2sum 如果数组是无序的,先排序(n*logn),然后用两个指针i,j,各自指向数组的首尾两端,令i=0,j=n-1,然后i++,j--,逐次判断a[i]+a[j]?=sum,如果某一刻a[i]+a ...

随机推荐

  1. HDU 1724 Ellipse 自适应simpson积分

    simpson公式是用于积分求解的比较简单的方法(有模板都简单…… 下面是simpson公式(很明显 这个公式的证明我并不会…… (盗图…… 因为一段函数基本不可能很规则 所以我们要用自适应积分的方法 ...

  2. ASP.NET网站怎么发布 Web项目程序怎么发布部署(暂时收藏)

    Web程序如何发布部署呢.网站项目做好了,需要发布出来,提交给客户,装上服务器.那怎么在ASP.NET开发环境中将网站程序发布出来呢 ^_^   工具/原料 Visual Studio 2010  ( ...

  3. html和html5学习

    html和html5学习 chorme.safari中的input或textarea html超链接(a)详细讲解 html5新增及删除标签 html表格 图片加alt属性 input的type属性 ...

  4. wpf xmal基础

    1.名称空间的引用 比如想使用System.Windows.Controls名称空间 首先需要把改名称空间所在的程序集presentationFramework.dll引用到项目里 然后在根元素的起始 ...

  5. ssh配置导致Ansible并发失败

    Ansible并发失败原因,  fork=100. 执行playbook时候没有并发 vim /usr/lib/python2.7/site-packages/ansible/runner/conne ...

  6. Java定时任务器

    java定时任务,每天定时执行任务.以下是这个例子的全部代码. public class TimerManager { //时间间隔 private static final long PERIOD_ ...

  7. C#操作xml文件进行增、删、改

    进行操作的xml文件: products.xml <?xml version="1.0" encoding="utf-8"?><product ...

  8. Python笔记6(异常)-20160924

    1. NameError 当视图访问一个未定义的变量则会发生NameError.

  9. FlatBuffers入门

    1.下载flatbuffers 从https://github.com/google/flatbuffers地址下载flatbuffers-master.zip文件. 2.编译flatbuffers ...

  10. redirect-action

    功能: 点击login , redirect 到hello.jsp 显示 "hello" 点击redirect, redirect 到 error.jsp 显示 "err ...