lintcode-medium-4 Sum

Posted 哥布林工程师

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了lintcode-medium-4 Sum相关的知识,希望对你有一定的参考价值。

Given an array S of n integers, are there elements ab,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.

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)

也和3 Sum基本一样。

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) {
        /* your code */
        ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
        
        if(numbers == null || numbers.length == 0)
            return result;
        
        Arrays.sort(numbers);
        
        for(int i = 0; i < numbers.length - 3; i++){
            if(i > 0 && numbers[i] == numbers[i - 1])
                continue;
            
            for(int j = i + 1; j < numbers.length - 2; j++){
                if(j > i + 1 && numbers[j] == numbers[j - 1])
                    continue;
                
                int left = j + 1;
                int right = numbers.length - 1;
                
                while(left < right){
                    if(numbers[i] + numbers[j] + numbers[left] + numbers[right] == target){
                        ArrayList<Integer> line = new ArrayList<Integer>();
                        line.add(numbers[i]);
                        line.add(numbers[j]);
                        line.add(numbers[left]);
                        line.add(numbers[right]);
                        result.add(new ArrayList<Integer>(line));
                        
                        left++;
                        while(left < right && numbers[left] == numbers[left - 1])
                            left++;
                        
                        right--;
                        while(left < right && numbers[right] == numbers[right + 1])
                            right--;
                    }
                    else if(numbers[i] + numbers[j] + numbers[left] + numbers[right] < target){
                        left++;
                        while(left < right && numbers[left] == numbers[left - 1])
                            left++;
                    }
                    else{
                        right--;
                        while(left < right && numbers[right] == numbers[right + 1])
                            right--;
                    }
                }
            }
        }
        
        return result;
    }
}

 

以上是关于lintcode-medium-4 Sum的主要内容,如果未能解决你的问题,请参考以下文章

leetcode_1292. Maximum Side Length of a Square with Sum Less than or Equal to Threshold_[二维前缀和](代码片段

append() 在这个代码片段中是如何工作的?与特定变量混淆[重复]

1049 数列的片段和

Python函数

hihocoder第233周

1104 Sum of Number Segments(二刷)