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:

  • Elements in a quadruplet (a,b,c,d) must be in non-descending order. (ie, a ≤ b ≤ c ≤ d)
  • 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)
代码如下:
 import java.util.*;
public class Solution {
public ArrayList<ArrayList<Integer>> fourSum(int[] num, int target) {
ArrayList<ArrayList<Integer>> all=new ArrayList<ArrayList<Integer>>();
Arrays.sort(num);
for(int i=0;i<num.length-3;i++){
if(num[i]+num[i+1]+num[i+2]+num[i+3]>target){
break;
}
if(num[i]+num[num.length-1]+num[num.length-2]+num[num.length-3]<target){
continue;
}
int result=target-num[i];
for(int j=i+1;j<num.length-2;j++){
if(num[i]+num[j]+num[j+1]+num[j+2]>target){
break;
}
if(num[i]+num[num.length-1]+num[num.length-2]+num[j]<target){
continue;
}
int result2=result-num[j];
int left=j+1,right=num.length-1;
while(left<right){
int sum=num[left]+num[right];
if(sum<result2){
left++;
}else if(sum>result2){
right--;
}else{
ArrayList<Integer> list=new ArrayList<Integer>();
list.add(num[i]);
list.add(num[j]);
list.add(num[left]);
list.add(num[right]);
if(!all.contains(list)){
all.add(list);
}
right--;
}
}
}
}
return all;
}
}
参考:https://www.nowcoder.com/profile/9319545/codeBookDetail?submissionId=24444004
https://www.cnblogs.com/grandyang/p/4130379.html

解题(Solution -4Sum)的更多相关文章

  1. LeetCode解题报告—— 4Sum & Remove Nth Node From End of List & Generate Parentheses

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

  2. 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  ...

  3. LeetCode解题报告--2Sum, 3Sum, 4Sum, K Sum求和问题总结

    前言: 这几天在做LeetCode 里面有2sum, 3sum(closest), 4sum等问题, 这类问题是典型的递归思路解题.该这类问题的关键在于,在进行求和求解前,要先排序Arrays.sor ...

  4. 【LeetCode】454. 4Sum II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 日期 题目地址:https://leetcod ...

  5. 【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  ...

  6. 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 = ...

  7. 《LeetBook》leetcode题解(18) : 4Sum[M]

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

  8. leetcode array解题思路

    Array *532. K-diff Pairs in an Array 方案一:暴力搜索, N平方的时间复杂度,空间复杂度N 数组长度为10000,使用O(N平方)的解法担心TLE,不建议使用,尽管 ...

  9. 【Leetcode】【Medium】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 ...

随机推荐

  1. ERROR: cannot launch node of type [teleop/teleop_key]: can't locate node [teleop_key] in package [teleop]

    节点由python写成,编译通过,运行时报错如下: ERROR: cannot launch node of type [teleop/teleop_key]: can't locate node [ ...

  2. VsCode配置go环境及插件安装

    在vscode中安装go插件. 安装git. 在%GOPATH%\src\目录下,建立golang.org文件夹,并再新建x文件夹. 目录为 "%GOPATH\src\golang.org\ ...

  3. ionic3使用@angular/http 访问nodejs(koa2框架)服务不能返回数据

    cordova的http插件不能使用在browser上,所以当需要在browser上浏览时,需要使用@angular/http 里的方法来访问nodejs服务. 如果出现服务端能够接收请求并相应,而客 ...

  4. 详解Asp.Net Core 2.1+的视图缓存(响应缓存)

    响应缓存Razor 页与 ASP.NET 核心 2.0 中不支持. 此功能将支持ASP.NET 核心 2.1 版本. 在老的版本的MVC里面,有一种可以缓存视图的特性(OutputCache),可以保 ...

  5. TCP/IP学习20180709-数据链路层-arp协议

    arp协议:address resolution protocol地址解析协议数据链路层,每个数据包都有MAC地址.主机是怎样根据对方主机的ip地址知道对方主机的mac地址呢?通过arp协议.主机里有 ...

  6. 在ubuntu中屏蔽“检测到系统程序出现问题”对话框

    ubuntu各个版本中都会时常遇到 “检测到系统程序出现问题”对话框 这是由于ubuntu系统中的“Apport”即错误信息的收集报告系统,将所有系统错误告警都不分大小和主次全部通知你,严重影响我们正 ...

  7. CentOS 与Ubuntu 下配置IP地址

    1.CentOS配置方法如下: 用vi打开配置文件 [root@haha3 ~]#  vi /etc/sysconfig/network-scripts/ifcfg-eth0 写入以下配置 DEVIC ...

  8. 借助ssh隧道和中间主机,使本地主机可以直连远程主机

    本地主机:    localhost 中间主机:   kickstart服务器 10.164.229.162 远程主机:   fuel 服务器  192.168.0.11 背景:正常情况下,本地不能直 ...

  9. 为什么我说IPFS社区从卖矿机开始,就是错的

    要回答这个问题,首先要了解去中心化存储项目和传统的区块链项目有什么区别.其中去中心化存储项目包括IPFS,基于IPFS的FileCoin.PPIO.Storj等. 传统区块链项目没有供需问题 首先以比 ...

  10. 数据库-Oracle的使用

    数据库的作用不言而喻,Oracle 作为一个主流的数据库,了解更是需要的 安装好oracle之后,登录初始账号,一般而言呢,我就新建一个用户, --创建新用户,用户名为JohnWu ,密码为 root ...