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)
算法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 ...
随机推荐
- 在ASP.NET Core 1.0中如何发送邮件
(此文章同时发表在本人微信公众号"dotNET每日精华文章",欢迎右边二维码来关注.) 题记:目前.NET Core 1.0中并没有提供SMTP相关的类库,那么要如何从ASP.NE ...
- 10g ASM下修改control file的位置
1.查看位置以及name是否正确 SQL> sho parameter name NAME TYPE VALUE ------------------------------------ --- ...
- Codeforces Round #337 (Div. 2) D. Vika and Segments 线段树 矩阵面积并
D. Vika and Segments Vika has an infinite sheet of squared paper. Initially all squares are whit ...
- Android自动化压力测试之Monkey Test 异常解读(五)
monkey结果分类 monkey结果详细解读 monkey运行log输出后,得读懂日志内容,定位错误 lgo日志顺序输出分别为 测试命令信息.随机事件流(11种事件).异常信息(anr.crash ...
- BurpSuite抓App数据包的方法
软件准备: 1.猎豹wifi 2.BurpSuite或者fillder都可以 查看电脑IP地址: 网卡ip: 确保无线网卡的IP和手机的代理IP保持一致即可
- JVM参数调优
JVM参数调优 JVM参数调优是一个很头痛的问题,可能和应用有关系,下面是本人一些调优的实践经验,希望对读者能有帮助,环境LinuxAS4,resin2.1.17,JDK6.0,2CPU,4G内存,d ...
- HDU 5769 后缀数组
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5769 [2016多校contest-4] 题意:给定一个字符,还有一个字符串,问这个字符串存在多少个不 ...
- jQuery-插件,优化
jQuery应用: 1.表单验证: A:jQuery Validation插件:有时需要将验证的属性写在class中,有时需要将验证信息写在属性中,例如: <input id="cem ...
- #include<>与#include""
对于头文件的包含,使用“<>”时,系统会到默认目录(编译器及环境变量,工程文件定义的头文件寻找目录,包括Qt安装的include目录)查找要包含的文件,这是标准方式: 用双引号时,系统先到 ...
- 贪心+模拟 Codeforces Round #288 (Div. 2) C. Anya and Ghosts
题目传送门 /* 贪心 + 模拟:首先,如果蜡烛的燃烧时间小于最少需要点燃的蜡烛数一定是-1(蜡烛是1秒点一支), num[g[i]]记录每个鬼访问时已点燃的蜡烛数,若不够,tmp为还需要的蜡烛数, ...