Given an array S of n integers, are there elements abc, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target.

Note: The solution set must not contain duplicate quadruplets.

For example, given array S = [1, 0, -1, 0, -2, 2], and target = 0.

A solution set is:
[
[-1, 0, 0, 1],
[-2, -1, 1, 2],
[-2, 0, 0, 2]
]

从数组中找出四个字之和为target的。因为有多组,所以要从头遍历到尾。

找出四个数之和为target,做法和三个数之和一样,就是外面多套一层循环。也就是 先排序,遍历数组,然后从剩下的数组中找出三个元素与当前元素之和为target,三个元素之和又是遍历剩下的数组,再从剩剩下的数组中找出两个数,使他们与前两个遍历的元素的和为target,这两个元素从两头往中间遍历。期间要跳过重复元素。具体见代码。每次遍历时还可以多加判断条件,如当前元素和最后三个元素 的和小于target,这时就进入下轮循环了,要使左边数字增大才行。

class Solution {
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(nums[i]+nums[i+1]+nums[i+2]+nums[i+3]>target) break;//前面四个都已经大于target了,就说明没有了(有序)
//当前数字和最后三个数字的和小于target,则将当前数字后移,也就是进行下一个循环
if(nums[i]+nums[nums.length-1]+nums[nums.length-2]+nums[nums.length-3]<target) continue;
if(i==0||(i>0&&nums[i]!=nums[i-1])){
for(int j=i+1;j<nums.length-2;j++){
if(nums[i]+nums[j]+nums[j+1]+nums[j+2]>target) break;
if(nums[i]+nums[j]+nums[nums.length-1]+nums[nums.length-2]<target) continue;
if(j==i+1||(j>i+1&&nums[j]!=nums[j-1])){
int left=j+1,right=nums.length-1,sum=target-nums[i]-nums[j];
while(left<right){
if(nums[left]+nums[right]==sum){
result.add(Arrays.asList(nums[i],nums[j],nums[left],nums[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]>sum) {
right--;
}else{
left++;
}
}
}
}
}
}
return result;
}
}

4 sum的更多相关文章

  1. LeetCode - Two Sum

    Two Sum 題目連結 官網題目說明: 解法: 從給定的一組值內找出第一組兩數相加剛好等於給定的目標值,暴力解很簡單(只會這樣= =),兩個迴圈,只要找到相加的值就跳出. /// <summa ...

  2. Leetcode 笔记 113 - Path Sum II

    题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...

  3. Leetcode 笔记 112 - Path Sum

    题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...

  4. POJ 2739. Sum of Consecutive Prime Numbers

    Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20050 ...

  5. BZOJ 3944 Sum

    题目链接:Sum 嗯--不要在意--我发这篇博客只是为了保存一下杜教筛的板子的-- 你说你不会杜教筛?有一篇博客写的很好,看完应该就会了-- 这道题就是杜教筛板子题,也没什么好讲的-- 下面贴代码(不 ...

  6. [LeetCode] Path Sum III 二叉树的路径和之三

    You are given a binary tree in which each node contains an integer value. Find the number of paths t ...

  7. [LeetCode] Partition Equal Subset Sum 相同子集和分割

    Given a non-empty array containing only positive integers, find if the array can be partitioned into ...

  8. [LeetCode] Split Array Largest Sum 分割数组的最大值

    Given an array which consists of non-negative integers and an integer m, you can split the array int ...

  9. [LeetCode] Sum of Left Leaves 左子叶之和

    Find the sum of all left leaves in a given binary tree. Example: 3 / \ 9 20 / \ 15 7 There are two l ...

  10. [LeetCode] Combination Sum IV 组合之和之四

    Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...

随机推荐

  1. C++对C的实用性增强

    #include <iostream> using namespace std; //C语言中的变量都必须在作用域开始的位置定义!! //C++中更强调语言的"实用性" ...

  2. UNIX网络编程——UDP 中的外出接口的确定

    已连接UDP套接字还可用来确定用于特定目的地的外出接口.这是由connect函数应用到UDP套接字时的一个副作用造成的:内核选择本地IP地址.这个本地IP地址通过为目的IP地址搜索路由表得到外出接口, ...

  3. Dynamics CRM 修改数据导出到EXCEL的最大条数

    系统默认的最大导出数为一万,这个数可以通过执行以下SQL看到,那要增加导出的最大数量改变MaxRecordsForExportToExcel的值即可. <span style="fon ...

  4. (NO.00004)iOS实现打砖块游戏(八):游戏中小球与砖块的碰撞

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请告诉我,如果觉得不错请多多支持点赞.谢谢! hopy ;) 现在回到GameScene.m中,我们所有的碰撞处理就放在该类中 ...

  5. 基于androidpn客户端修改的AndroidPNClient

    最近在做推送,采用的框架是androidpn,但对于客户端实在是修改得受不了了,特别是重连和连接那一块,有些BUG的修改实在是难以下手,比如在重连那里,原来的写法是在死循环中不断调用 xmppMana ...

  6. ADFS3.0 Customizing the AD FS Sign-in Pages

    Windows Server2012R2自带的adfs是3.0的版本,不同于以前的版本的是3.0中登陆页面的定制化全部是通过powershell指令实现,官方的介绍链接如下:http://techne ...

  7. Spark MLlib数据类型

        MLlib支持几种数据类型:本地向量(local vectors),和存储在一个简单机器中的矩阵(matrices),以及由一个或多个RDDs组成的分布式矩阵. 1,本地向量(Local Ve ...

  8. Java 8新特性探究(二)类型注解和重复注解

    本文将介绍java 8的第二个特性:类型注解. 注解大家都知道,从java5开始加入这一特性,发展到现在已然是遍地开花,在很多框架中得到了广泛的使用,用来简化程序中的配置.那充满争议的类型注解究竟是什 ...

  9. 自建log4cxx.sln

    log4cxx的下载包是0.10版本的,代码较旧,有些错误,应该从主干下载最新的代码. 0.10的下载包中有projects目录,内有VC6的工程文件; 还有site目录,内有vc构建的帮助文档vst ...

  10. C#之概述

    当前流行的开发语言概述 C#是微软公司为Visual Studio开发平台推出的一种简洁.类型安全的面向对象的编程语言,开发人员可以通过她编写在.NET Framework上运行的各种安全可靠的应用程 ...