lintcode:四个数之和

题目

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

样例

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

(-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个值和正确答案是一样的。至于为什么我还是不明白,四个数的顺序应该是没有影响的,如果不去重只是多运行几次,但是不至于报错的。。。。但是这里还是有了影响。

原文地址:https://www.cnblogs.com/theskulls/p/4915752.html