题目

给一个包含n个数的整数数组S,在S中找到所有使得和为给定整数target的四元组(a, b, c, d)。

样例

例如,对于给定的整数数组S=[1, 0, -1, 0, -2, 2] 和 target=. 满足要求的四元组集合为:

(-1, 0, 0, 1)

(-2, -1, 1, 2)

(-2, 0, 0, 2)

注意

四元组(a, b, c, d)中,需要满足a <= b <= c <= d

答案中不可以包含重复的四元组。

解题

怎么感觉下面程序已经没法修改了但是在39%测试数据时候超时

public class Solution {
/**
* @param numbers : Give an array numbersbers of n integer
* @param target : you need to find four elements that's sum of target
* @return : Find all unique quadruplets in the array which gives the sum of
* zero.
*/
public ArrayList<ArrayList<Integer>> fourSum(int[] numbers, int target) {
//write your code here
ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
if(numbers == null || numbers.length < 4)
return result;
Arrays.sort(numbers);
for(int i=0;i< numbers.length - 1 ; i++){
if(numbers[i]==numbers[i+1])
continue;
for(int j = numbers.length - 1;j>i ;j--){
if(numbers[j] == numbers[j-1])
continue;
int left = i + 1;
int right = j - 1;
int n = target - numbers[i] - numbers[j];
while(left< right){
while(numbers[left] == numbers[++left] && left<right);
left--;
while(numbers[right] == numbers[--right] && left<right);
right++; int sum = numbers[left] + numbers[right];
if(n==sum){
ArrayList<Integer> path = new ArrayList<Integer>();
path.add(numbers[i]);
path.add(numbers[left]);
path.add(numbers[right]);
path.add(numbers[j]);
if(!result.contains(path))
result.add(path);
}
else if(sum<target){
left++;
}else{
right--;
}
}// end while
}// end for
}// end for
return result;
}
}

Java Code

九章上面的代码,感觉和上面的明明差不多,但是就能通过

public class Solution {
public ArrayList<ArrayList<Integer>> fourSum(int[] num, int target) {
ArrayList<ArrayList<Integer>> rst = new ArrayList<ArrayList<Integer>>();
Arrays.sort(num); for (int i = 0; i < num.length - 3; i++) {
if (i != 0 && num[i] == num[i - 1]) {
continue;
} for (int j = i + 1; j < num.length - 2; j++) {
if (j != i + 1 && num[j] == num[j - 1])
continue; int left = j + 1;
int right = num.length - 1;
while (left < right) {
int sum = num[i] + num[j] + num[left] + num[right];
if (sum < target) {
left++;
} else if (sum > target) {
right--;
} else {
ArrayList<Integer> tmp = new ArrayList<Integer>();
tmp.add(num[i]);
tmp.add(num[j]);
tmp.add(num[left]);
tmp.add(num[right]);
rst.add(tmp);
left++;
right--;
while (left < right && num[left] == num[left - 1]) {
left++;
}
while (left < right && num[right] == num[right + 1]) {
right--;
}
}
}
}
} return rst;
}
}

Java Code

在对比两个程序发现,确实有错误,最重要的还是,在sum==target 的时候left和right 没有变化,其他的小错误是在参考网上程序时候没有改完

改成下面的程序但是还是在78% 的测试数据时候有错误。

public class Solution {
/**
* @param numbers : Give an array numbersbers of n integer
* @param target : you need to find four elements that's sum of target
* @return : Find all unique quadruplets in the array which gives the sum of
* zero.
*/
public ArrayList<ArrayList<Integer>> fourSum(int[] numbers, int target) {
//write your code here
ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
if(numbers == null || numbers.length < 4)
return result;
Arrays.sort(numbers);
for(int i=0;i< numbers.length - 4 ; i++){
for(int j = numbers.length - 1;j>i ;j--){
int left = i + 1;
int right = j - 1;
while(left< right){ int sum = numbers[i] + numbers[j] + numbers[left] + numbers[right];
if(target==sum){
ArrayList<Integer> path = new ArrayList<Integer>();
path.add(numbers[i]);
path.add(numbers[left]);
path.add(numbers[right]);
path.add(numbers[j]);
if(!result.contains(path))
result.add(path);
left++;
right--;
}
else if(sum<target){
left++;
}else{
right--;
}
}// end while
}// end for
}// end for
return result;
}
}

Java Code

在网上看到别人程序对第一四个数据进行去重,感觉这个是有问题的,比如:-1 -1 1 1, -1 -1 -1 3  这个也是个答案的,但是上面九章给的程序确实是由去重的操作。。。。

仔细看九章的程序,发现:

1,i是第一个数,j是第二个数,Start是第三个数,End是第四个数

2.,前两个数进行了去重,对计算过的Start和End也进行了去重,start和End去重很好理解,但是对于前两个数去重我就不能理解了

3.上面78%报错的程序修改成九章的,在39%处报错,去除前两个去重的程序,还是在78%出报错,但是我发现在target==92的时候我的程序输出的结果有75个值和正确答案是一样的。至于为什么我还是不明白,四个数的顺序应该是没有影响的,如果不去重只是多运行几次,但是不至于报错的。。。。但是这里还是有了影响。

lintcode:四个数之和的更多相关文章

  1. 18 4Sum(寻找四个数之和为指定数的集合Medium)

    题目意思:给一个乱序数组,在里面寻找三个数之和为target的所有情况,这些情况不能重复,增序排列 思路:采用3Sum的做法 ps:有见一种用hash的,存任意两个元素的和,然后变成3sum问题,需要 ...

  2. [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 ...

  3. ACM_四数之和

    四数之和 Time Limit: 2000/1000ms (Java/Others) Problem Description: 有n个不同的整数,判断能否从中选4次,4个数和刚好为m.数字可重复选取. ...

  4. [LeetCode] 454. 4Sum II 四数之和之二

    Given four lists A, B, C, D of integer values, compute how many tuples (i, j, k, l) there are such t ...

  5. [LeetCode] 454. 4Sum II 四数之和II

    Given four lists A, B, C, D of integer values, compute how many tuples (i, j, k, l) there are such t ...

  6. 【LeetCode】 454、四数之和 II

    题目等级:4Sum II(Medium) 题目描述: Given four lists A, B, C, D of integer values, compute how many tuples (i ...

  7. 【LeetCode】四数之和

    [问题]给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 target 相等?找 ...

  8. Leetcode(18)-四数之和

    给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 target 相等?找出所有满 ...

  9. 【LeetCode】18. 4Sum 四数之和

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:four sum, 4sum, 四数之和,题解,leet ...

随机推荐

  1. js----方法是否加括号的问题

    在我们js编写程序的时候,我们会写很多函数然后调用它们,那么这些函数调用的时候什么时候加()什么时候不加()?记住以下几个要点. (1)函数做参数时都不要括号. function fun(e) { a ...

  2. vs2008中使用Newtonsoft.Json

    异常:找不到方法:“Boolean System.Runtime.Serialization.DataContractAttribute.get_IsReference()” 在使用Newtonsof ...

  3. Python开发【第一篇】Python基础之反射

    反射 反射的作用:反射得作用是提高代码可读行. __import__导入模块和import导入模块的区别: __import__导入模块是通过字符串进行导入. import是常用得导入模块方法. 扩展 ...

  4. Redis集群明细文档

    Redis目前版本是没有提供集群功能的,如果要实现多台Redis同时提供服务只能通过客户端自身去实现(Memchached也是客户端实现分布式).目前根据文档已经看到Redis正在开发集群功能,其中一 ...

  5. git add 命令详解

    或"表示git会例出索引库中的文件列表中的第个文件."-"表示git会例出索引库中的文件列表中的第个文件到第个文件.回车将执行.如果我们不输入任何东西,直接回车,将结束r ...

  6. dblink 的源数据表结构修改后在存储过程里执行报错

    原情况:A服务器表A服务器B也有一张表A服务器B上有一个存储过程要把本地的head表向A服务器表里插入数据.变更后:在A服务器表里增加了一个字段inserttime,服务器B存储过程本地表向A服务器插 ...

  7. 自定义PopupWindow 怎么设置PopupWindow的宽度充满全屏宽度

    自定义了一个MyPopMenu类,用于上图中的下拉筛选效果的. 但是按照网上有说需要: new PopupWindow(view,getWindowManager().getDefaultDispla ...

  8. Useful related java API for Android

    Language_suport and Other Language-Oriented API: strings,exceptions, threads, #java.lang.* offers th ...

  9. Google history

    传说,硅谷的公司在和微软的竞争中一直处于下风,不论在市场,人才,还是在打官司上,直到婴儿巨人Baby Giant谷歌的出现,历史才出现前所未有的改变.Google以一个强大的挑战者的身份出现在人们的视 ...

  10. 将Asp.Net MVC应用程序的控制器定义在单独的程序集(类库)中

    一直以来都想把控制器的代码部署到单独的程序集里.昨天研究Asp.Net MVC的源代码,偶然发现有一个奇特的类“ControllerBuilder”,MSDN上的介绍相当简略,就一句话“表示一个类,该 ...