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)
算法1:排序后,固定前两个数,后两个数适用双指针,O(N^3).
java:
public class Solution {
public List<List<Integer>> fourSum(int[] num, int target) {
int len=num.length;
Arrays.sort(num);
List<List<Integer>> list =new LinkedList<List<Integer>>(); int i=0;
while(i<len-3){
int j=i+1;
while(j<len-2){
int k=j+1;
int l=len-1;
while(k<l){
int sum = num[i]+num[j]+num[k]+num[l];
if(sum==target){
List<Integer> lst = new LinkedList<Integer>();
lst.add(num[i]);
lst.add(num[j]);
lst.add(num[k]);
lst.add(num[l]);
list.add(lst); while(k+1<len-1&&num[k]==num[k+1]){
k++;
}
k++;
while(l-1>2&&num[l]==num[l-1]){
l--;
}
l--;
}else if(sum<target){
k++;
}else{
l--;
}
}
while(j+1<len-2&&num[j]==num[j+1])
j++;
j++;
}
while(i+1<len-3&&num[i]==num[i+1])
i++;
i++;
}
return list;
}
}
4Sum的更多相关文章
-
[LeetCode] 4Sum II 四数之和之二
Given four lists A, B, C, D of integer values, compute how many tuples (i, j, k, l) there are such t ...
-
[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 = tar ...
-
LeetCode:3Sum, 3Sum Closest, 4Sum
3Sum Closest Given an array S of n integers, find three integers in S such that the sum is closest t ...
-
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 ...
-
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 = ...
-
6.3Sum &;&; 4Sum [ &;&; K sum ] &;&; 3Sum Closest
3Sum Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find a ...
-
3Sum &; 4Sum
3 Sum Given an array S of n integers, are there elements a, b, c in Ssuch that a + b + c = 0? Find a ...
-
【leetcode】4Sum
4Sum Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d ...
-
2sum、3sum、4sum以及任意连续的数的和为sum、任意连续或者不连续的数的和为sum
2sum 如果数组是无序的,先排序(n*logn),然后用两个指针i,j,各自指向数组的首尾两端,令i=0,j=n-1,然后i++,j--,逐次判断a[i]+a[j]?=sum,如果某一刻a[i]+a ...
-
求和问题总结(leetcode 2Sum, 3Sum, 4Sum, K Sum)
转自 http://tech-wonderland.net/blog/summary-of-ksum-problems.html 前言: 做过leetcode的人都知道, 里面有2sum, 3sum ...
随机推荐
-
C语言 &#183; 回文数
问题描述 1221是一个非常特殊的数,它从左边读和从右边读是一样的,编程求所有这样的四位十进制数. 输出格式 按从小到大的顺序输出满足条件的四位十进制数. 方案一: int main(){ int ...
-
利用带关联子查询Update语句更新数据
Update是T-sql中再简单不过的语句了,update table set column=expression [where condition],我们都会用到.但update的用法不仅于此,真 ...
-
Java 对文件的操作
public class ReadFile { /** * 按行读取文件操作 * @throws IOException */ public void readFile(String fileName ...
-
iOS开发之----常用函数和常数
介绍一下Objective-c常用的函数,常数变量 算术函数 [算术函数] 函数名 说明 int rand() 随机数生成.(例)srand(time(nil)); //随机数初期化int val = ...
-
javaWeb 在jsp中 使用自定义标签输出访问者IP
1.java类,使用简单标签,jsp2.0规范, 继承 SimpleTagSupport public class ViewIpSimpleTag extends SimpleTagSupport { ...
-
hdu 5437 Alisha’s Party 优先队列
Alisha’s Party Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/contests/contest_sh ...
-
纯CSS滑动效果
原文地址:Pure CSS Slide Up and Slide Down 示例地址:Pure CSS Slide Demo 原文日期: 2013年08月26日 翻译日期: 2013年08月27日 如 ...
-
CAN总线基础知识(三)
1.CAN协议 1.1 帧类型 通讯时使用下面5个类型的帧: 数据帧 遥控帧 错误帧 过载帧 帧间空隙 在所有这些帧中,数据帧和遥控帧由用户设置,而其它帧则由CAN硬件设置. 数据和遥控帧有两种格式: ...
-
SpringBoot jar包中资源加载问题
在IDE下调试怎么也没有发现问题,但是部署到服务器上,提示找不到资源,找了半天资料总算是找到了原因: Jar包中的资源加载不能使用File方式,只能使用InputStream方式读取.知道原因就好解决 ...
-
【Thymeleaf】Thymeleaf模板对没有结束符的HTML5标签解析出错的解决办法
解决方案 spring: thymeleaf: mode: LEGACYHTML5 <dependency> <groupId>net.sourceforge.nekohtml ...