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. Ubuntu上的MySQL可以远程访问

    1. 3306端口是不是没有打开? 使用nestat命令查看3306端口状态: ~# netstat -an | grep 3306 tcp        0      0 127.0.0.1:330 ...

  2. Spring编程式事务管理和声明式事务管理

    本来想写一篇随笔记一下呢,结果发现一篇文章写的很好了,已经没有再重复写的必要了. https://www.ibm.com/developerworks/cn/education/opensource/ ...

  3. Centos7下GlusterFS分布式存储集群环境部署记录

    0)环境准备 GlusterFS至少需要两台服务器搭建,服务器配置最好相同,每个服务器两块磁盘,一块是用于安装系统,一块是用于GlusterFS. 192.168.10.239 GlusterFS-m ...

  4. Odoo-10开发环境配置与测试

    Odoo是使用Python写的开源ERP软件,这几年比较火.内部有实施能力的,这个软件还是很不错的.总体来说,国外的这类软件,更多是在做平台(比如微软的AX.SharePoint.SAP等)平台本身具 ...

  5. Hbase的集群安装

    hadoop集群的搭建 搭建真正的zookeeper集群 Hbase需要安装在成功部署的Hadoop平台,并且要求Hadoop已经正常启动. 同时,HBase需要集群部署,我们分别把HBase 部署到 ...

  6. 0008 合并K个排序链表

    合并 k 个排序链表,返回合并后的排序链表.请分析和描述算法的复杂度. 示例: 输入: [   1->4->5,   1->3->4,   2->6 ] 输出: 1-&g ...

  7. 接口与继承:方法覆盖(super)

    源代码 //父类Parent class Parent{ int x; int y; Parent() { x = ; y = ; } public void Set(int a,int b) { x ...

  8. RN-环境配置

    良好的开端是成功的一半,这是window平台安装步骤 首先配置JDK1.8  配置JAVA_HOME环境变量 然后安装Android Studio3.2 然后安装react-native-cli np ...

  9. Python 内置函数math,random

    内置函数的一些操作 - math(数学模块) - random(随机模块) - 使用内置函数时注意需要导入 math - (ceil)向上取整,返回取整数 # 向上取整,返回向上取整的数 import ...

  10. React Native仿京东客户端实现(首页 分类 发现 购物车 我的)五个Tab导航页面

    1.首先创建  这几个文件 myths-Mac:JdApp myth$ yarn add react-native-tab-navigator 2.各个文件完整代码 1)CartPage.js imp ...