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 ...
随机推荐
- PMP 第六章 项目时间管理
定义活动 排列活动顺序 估算活动资源 估算活动持续时间 制定进度计划 控制进度计划 1.进度管理计划和进度计划的内容分别是什么,有什么区别? 进度计划:项目各活动计划完成日期的编排. 进度管理计 ...
- OVER(PARTITION BY)函数用法
OVER(PARTITION BY)函数介绍 开窗函数 Oracle从8.1.6开始提供分析函数,分析函数用于计算基于组的某种聚合值,它和聚合函数的不同之处是:对于每个组返 ...
- 在Salesforce中向Page Layout中添加Visualforce Page
在Salesforce中可以向Object所对应的Layout中添加我们自定义的Visualforce Page. 此时的Visualforce Page与Asp.Net MVC中的Partial V ...
- HDU 5791 Two DP
Two Problem Description Alice gets two sequences A and B. A easy problem comes. How many pair of ...
- ios摇一摇截屏代码
#import "ViewController.h" @interface ViewController () @end @implementation ViewControlle ...
- whl文件安装
进入whl文件的目录,直接pip install ...即可
- flex模拟微信布局
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8&quo ...
- Jmeter之参数化(五)
Jmeter用函数(__Random)批量添加的用法 首先点击 选项----->函数助手对话框-----> 这个是随机取值的意思 在这边输入取值的范围 然后点生成 即可 例如 从 数 ...
- Popupwindow 的简单实用,(显示在控件下方)
第一步: private PopupWindow mPopupWindow; 第二步:写一个popupwindow的布局文件XML <?xml version="1.0" e ...
- MVC3实现多个按钮提交
有时我们需要在同一个Form表单中进行多个按钮的提交,来完成不同的功能.因为MVC中一个表单默认只能提交到一个Action处理,相对比较麻烦. 1.使用客户端脚本 我们可以view中加上如下代码: & ...