4Sum --- LeetCode
来源:互联网 发布:なごみ 知乎 编辑:程序博客网 时间:2024/05/21 15:03
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)
解题思路:使用两重循环,同3sum,二分查找。注意去重。
public class Solution {
public void sort(int[] num, int start, int end){
int left=start;
int right=end;
int data=num[left];
while(left<right){
while(left<right&&num[right]>=data){
right--;
}
if(left<right){
num[left]=num[right];
}
while(left<right&&num[left]<=data){
left++;
}
if(left<right){
num[right]=num[left];
}
}
num[left]=data;
if(start<left-1){
sort(num,start,left-1);
}
if(end>left+1){
sort(num,left+1,end);
}
}
public List<List<Integer>> fourSum(int[] num, int target) {
List<List<Integer>> rs=new ArrayList<List<Integer>>();
int sum=0;
int len=num.length;
if(num==null){
return null;
}
if(len<4){
return rs;
}
sort(num,0,len-1);
for(int i=0;i<=len-4;i++){
int j=i+1;
while(j<=len-3){
sum=num[i]+num[j];
int left=j+1;
int right=len-1;
while(left<right){
int diff=num[left]+num[right]+sum;
if(diff>target){
right--;
}else if(diff<target){
left++;
}else{
List<Integer> data=new ArrayList<Integer>();
data.add(num[i]);
data.add(num[j]);
data.add(num[left]);
data.add(num[right]);
if(!rs.contains(data)){
rs.add(data);
}
left++;
right--;
}
}
j++;
}
}
return rs;
}
}
public void sort(int[] num, int start, int end){
int left=start;
int right=end;
int data=num[left];
while(left<right){
while(left<right&&num[right]>=data){
right--;
}
if(left<right){
num[left]=num[right];
}
while(left<right&&num[left]<=data){
left++;
}
if(left<right){
num[right]=num[left];
}
}
num[left]=data;
if(start<left-1){
sort(num,start,left-1);
}
if(end>left+1){
sort(num,left+1,end);
}
}
public List<List<Integer>> fourSum(int[] num, int target) {
List<List<Integer>> rs=new ArrayList<List<Integer>>();
int sum=0;
int len=num.length;
if(num==null){
return null;
}
if(len<4){
return rs;
}
sort(num,0,len-1);
for(int i=0;i<=len-4;i++){
int j=i+1;
while(j<=len-3){
sum=num[i]+num[j];
int left=j+1;
int right=len-1;
while(left<right){
int diff=num[left]+num[right]+sum;
if(diff>target){
right--;
}else if(diff<target){
left++;
}else{
List<Integer> data=new ArrayList<Integer>();
data.add(num[i]);
data.add(num[j]);
data.add(num[left]);
data.add(num[right]);
if(!rs.contains(data)){
rs.add(data);
}
left++;
right--;
}
}
j++;
}
}
return rs;
}
}
0 0
- 【Leetcode】4Sum (Sum)
- leetcode 2 sum 3sum 4sum
- Leetcode 2SUM-3SUM-4SUM
- Leetcode-2sum,3sum,4sum
- leetcode 2 sum & 3 sum & 4 sum
- [LeetCode] 2Sum, 3Sum, 4Sum, 3SUm closet
- [LeetCode] K sum(2Sum、3Sum、4Sum)
- leetcode--sum集合:2sum,3sum,4sum
- leetcode --- 2 sum , 3 sum , 4 sum , k sum problem
- LeetCode: 4 Sum
- LeetCode: 4Sum
- LeetCode 4Sum
- Leetcode: 4SUM
- 【leetcode】 4 sum
- LeetCode 4Sum
- leetcode 44: 4Sum
- [Leetcode] 4 Sum [Unsolved]
- 【leetcode】4Sum
- MFC操作ini文件方法
- 【BZOJ1002】【FJOI2007】轮状病毒 生成树计数推导。 Python代码
- 深入理解 AngularJS 的 Scope
- 取球算法
- Linux下搭建SVN服务器
- 4Sum --- LeetCode
- AngularJS性能调试的7个建议
- Linux MySQL Apache Django配置
- Linux一些最基本命令
- IOCP12 WSASendTo不存在的目标
- hadoop的kerberos认证
- 代码中特殊的注释技术——TODO、FIXME和XXX的用处
- 正确配置Linux系统ulimit值的方法
- 状态栏高度改变 tabbar高度问题