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. 关于eth0 eth0:1 和eth0.1关系介绍

    eth0 eth0:1 和eth0.1三者的关系对应于物理网卡.子网卡.虚拟VLAN网卡的关系:物理网卡:物理网卡这里指的是服务器上实际的网络接口设备,这里我服务器上双网卡,在系统中看到的2个物理网卡 ...

  2. tp框架增删改

    选择一张表: 首先要创建模型: 1 $n = M("account"); 数据库添加数据: 1.使用数组: 1 2 3 1.使用数组 $arr = array("uid& ...

  3. kafka性能调优(转)

    原文  https://blog.csdn.net/weixin_39478115/article/details/79155287 Broker参数配置 1.网络和io操作线程配置优化 # brok ...

  4. FuzzScanner 信息收集小工具

    前言: 该工具集成了各种大牛的工具,比如子域名发现,目录扫描,nmap端口扫描,c段地址查询,端口指纹,以及waf查询 00X1: 安装不推荐git安装,首先直接githup脱下来:git clone ...

  5. SA vs NSA

    5G: What is Standalone (SA) vs Non-Standalone (NSA) Networks? According to the recent 3GPP Release 1 ...

  6. Linux 的文件软链接如何删除

    Linux 的文件软链接如何删除创建软链接即用 ln -s 原始文件或文件夹 目标文件或文件夹 举例:[root@recover test]# pwd/test[root@recover test]# ...

  7. Unity的Write Defaults->从一个例子谈起

    Write Defaults是什么? 在Unity的Animator中点击任何一个手动创建的State,我们就会在Inspector面板中看到下图的WriteDefaults选项 (图1,Animat ...

  8. UnityShaderVariant的一些探究心得

    最近遇到了一个问题,角色在Unity编辑器里运行渲染结果都是好的,打包到IOS上却发现,角色身上渲染的很黑.花了些时间查了查,又试了试,把这方面算是初步弄清楚了. 先说出现问题的原因,由于我们把sha ...

  9. int main(int argc,char* argv[]) 的含义和用法

    1.基本概念 argc,argv 用命令行编译程序时有用. 主函数main中变量(int argc,char *argv[ ])的含义,有些编译器允许将main()的返回类型声明为void,这已不再是 ...

  10. 利用JAVA API函数实现数据的压缩与解压缩

      综述 许多信息资料都或多或少的包含一些多余的数据.通常会导致在客户端与服务器之间,应用程序与计算机之间极大的数据传输量.最常见的解决数据存储和信息传送的方法是安装额外的存储设备和扩展现有的通讯能力 ...