解题(Solution -4Sum)
Given an array S of n integers, are there elements a, b, c, 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)的更多相关文章
- 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 + ...
- 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 ...
- LeetCode解题报告--2Sum, 3Sum, 4Sum, K Sum求和问题总结
前言: 这几天在做LeetCode 里面有2sum, 3sum(closest), 4sum等问题, 这类问题是典型的递归思路解题.该这类问题的关键在于,在进行求和求解前,要先排序Arrays.sor ...
- 【LeetCode】454. 4Sum II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 日期 题目地址:https://leetcod ...
- 【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 ...
- 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 = ...
- 《LeetBook》leetcode题解(18) : 4Sum[M]
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- leetcode array解题思路
Array *532. K-diff Pairs in an Array 方案一:暴力搜索, N平方的时间复杂度,空间复杂度N 数组长度为10000,使用O(N平方)的解法担心TLE,不建议使用,尽管 ...
- 【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 ...
随机推荐
- JAVA中的算法
线性排序 冒泡排序 package com.jiedada.creat; public class maopao { public static void main(String[] args) { ...
- 用anaconda安装tensorflow
conda create -n tensorflow python=2.7 conda activate tensorflow / source activate tensorflow anacond ...
- 华为PAY公交卡建议开卡免费!
华为PAY公交卡,大家都知道是华为与当地交通卡通公司合作的,开卡费大概15-29元,最低充值10-30元. 估计大部分开卡费是给了当地交通卡公司,华为也应该有收入分成.这个虚拟公交卡,不同于实体公交卡 ...
- WIN7以上系统安装VB6的解决办法,附上个批处理。
一.安装时显示oledb32r.dll不能在系统注册数据库中注册在C盘查找oledb32r.dll,改名为oledb32r-2.dll即可,dll冲突了. 二.WIN7安装VB6提示VB98ENT.S ...
- mysql binlog抽取某个表的数据
1.先把binlog文件转化为sql --base64-output=decode-rows -v > /var/mydiag.sql 2.抽取某一个表的数据 grep -B0 -A27 -w ...
- springmvc读取服务器磁盘图片,显示于前台页面
在项目中的config目录下有一个文件,在后台程序中获取 它并使用. springmvc提供一个方法:File file = new ClassPathResource("NonTaxVou ...
- MySQL查询语句报错 sql_mode=only_full_group_by 问题
升级MySQL到5.7后,查询语句总是报sql_mode=only_full_group_by问题,总结归纳了两种解决方案,推存第二种解决方案. 报错信息: [Err] 1055 - Expressi ...
- hex转mif文件 verilog
用FPGA来跑ARM 核的时候,刚开始将Keil编译产生的hex文件拿来仿真和下到板子上的时候,发现程序运行不正确.细细观察仿真波形发现,在Altera的ROM IP中直接调用Keil产生的hex文件 ...
- Redis深入学习笔记(四)主从数据复制流程
主从节点的数据复制是Redis高可用和高负载的重要基础,本篇介绍数据的主从复制流程. 数据复制策略: 全量复制:一般用于初次复制场景,Redis早期支持的复制功能只有全量复制,它会把主节点全部数据一次 ...
- 大数据学习之路(1)Hadoop生态体系结构
Hadoop的核心是HDFS和MapReduce,hadoop2.0还包括YARN. Hadoop1.x的生态系统: Hadoop2.x引入YARN: HDFS(Hadoop分布式文件系统)源自于Go ...