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语言省略extern的缺陷

    在一个文件中(比如a.c)定义一个全局变量int a = 10; 然后在另一个代码文件(比如main.c)中需要使用变量a,可以写 int a; 单独看main.c文件时就会出现二义性,一个含义是当其 ...

  2. Google会思考的深度学习系统

    上周五在旧金山举行的机器学习会议上,Google软件工程师Quoc V. Le讲解了Google的"深度学习"系统是如何运作的. "深度学习"需要用到大型计算机 ...

  3. UNIX网络编程——send与recv函数详解

    #include <sys/socket.h> ssize_t recv(int sockfd, void *buff, size_t nbytes, int flags); ssize_ ...

  4. Android初级教程实现电话录音

    需求:设置来电后自动录音. 首先设置一个按钮,代码很简单这里就不再给出. 建一个类,RecorderServicer extends Service package com.ydl.recorder; ...

  5. Android PackageManager源码浅析以及静默安装实现方式

    Aandroid应用管理    >http://blog.csdn.net/sk719887916/article/details/50314017 skay整理.        >201 ...

  6. 利用openssl管理证书及SSL编程第3部分:将MinGW编译的openssl dll导出def和lib供MSVC使用

    将MinGW编译的openssl dll导出def和lib供MSVC使用 前面我们用mingw把openssl 编译成了动态库,得到下面2个dll文件: libeay32.dll ssleay32.d ...

  7. 分布式进阶(三)Ubuntu 14.04 之JDK安装

    Ubuntu 10.04 之JDK安装 1.从官网 (http://www.Oracle.com/technetwork/java/javase/downloads/jdk6u35-downloads ...

  8. 【嵌入式开发】 Linux Kernel 下载 配置 编译 安装 及 驱动简介

    作者 : 韩曙亮 转载请出名出处 : http://blog.csdn.net/shulianghan/article/details/38636827 一. Linux 内核简介 1. 内核功能简介 ...

  9. log4j.xml示例

    <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE log4j:configuration SY ...

  10. 【翻译】针对多种设备定制Ext JS 5应用程序

    原文:Tailoring Your Ext JS 5 Application for a Multi-Device World 概述 鉴于当今设备和表单因素的扩散,要针对所有这些可能性来优化应用程序已 ...