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 ...
随机推荐
- Codeforces Round #103 (Div. 2) D. Missile Silos(spfa + 枚举边)
题目链接:http://codeforces.com/problemset/problem/144/D 思路:首先spfa求出中心点S到其余每个顶点的距离,统计各顶点到中心点的距离为L的点,然后就是要 ...
- Redis主从自动failover
Redis主从架构持久化存在一个问题,即前次测试的结论,持久化需要配置在主实例上才能跨越实例保证数据不丢失,这样以来主实例在持久化数据到硬 盘的过程中,势必会造成磁盘的I/O等待,经过实际测试,这个持 ...
- 【前台 】字符串和js对象的相互转化
利用原生JSON对象,将对象转为字符串 var jsObj = {}; jsObj.testArray = [1,2,3,4,5]; jsObj.name = 'CSS3'; jsObj.date = ...
- Bootstrap Table 表格参数详解
表格参数 名称 标签 类型 默认 描述 - data-toggle String 'table' 不用写 JavaScript 直接启用表格. classes data-class ...
- JQuery学习之语法
1.JQuery语法就是通过选取HTML元素,并对选取的元素执行某些操作: 基础语法:$(selector).action() (1)美元符号定义jQuery (2)选择符(selector)查询和查 ...
- 阻止Ajax多次提交
1.Ajax的abort() xhr = $.ajax({}) if (xhr){ xhr.abort(); } 2.通过在Ajax的beforeSend()方法以及complete()方法添加删除类 ...
- 4、delphi record数组复制
SetLength(OldDeptInfo,0); //释放旧数组 OldDeptInfo:=nil; 这样也可以: //SetLength(OldDeptInfo,Length(NewDeptInf ...
- 学习资源asp.net
http://www.runoob.com ajax 同一表单,多部分提交.增加,修改,删除 服务器端控件: http://technet.microsoft.com/zh-cn/library/cc ...
- 【转】Myeclipse建立Maven项目
原文地址: http://b-l-east.iteye.com/blog/1246482 1. 使用Maven创建webapp工程----原因是使用Maven时一般需要遵循一定的目录结构,虽然也可以使 ...
- [转载]DW数据仓库建模与ETL的实践技巧
一.Data仓库的架构 Data仓库(Data Warehouse DW)是为了便于多维分析和多角度展现而将Data按特定的模式进行存储所建立起来的关系型Datcbase,它的Data基于OLTP源S ...