*?[lintcode]4Sum

Given an array S of n integers, are there elements abc, and d in S such that a + b + c + d = target?

Find all unique quadruplets in the array which gives the sum of target.

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,2始终在数组左侧对数组进行遍历。指针3,4在剩下区间的两侧,根据与target的大小关系进行移动。

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) {
        ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>> ();
        Arrays.sort(numbers);
        
        if(numbers.length < 3) return res;
        
        for(int left = 0; left < numbers.length - 3; left ++) {
            for(int G = left + 1; G < numbers.length - 2; G ++) {
                int i = G + 1, j = numbers.length - 1;
                while(i < j) {
                    if(numbers[left] + numbers[G] + numbers[i] + numbers[j] > target) j--;
                    else if(numbers[left] + numbers[G] + numbers[i] + numbers[j] < target) i++;
                    else {
                            ArrayList<Integer> tmp = new ArrayList<Integer>();
                            tmp.add(numbers[left]);
                            tmp.add(numbers[G]);
                            tmp.add(numbers[i]);
                            tmp.add(numbers[j]);
                            res.add(tmp);
                            while(j - 1 > i && numbers[j] == numbers[j - 1])
                                j--;
                            
                            while(i + 1 < j && numbers[i] == numbers[i + 1])
                                i++;
                            
                            i++;j--;
                    }
                }
                while(G + 1 < numbers.length - 2 && numbers[G] == numbers[G + 1]) G++;
            }
            while(left + 1 < numbers.length && numbers[left] == numbers[left + 1]) left++;
        }
        return res;
    }
}


下面的方法是两个的指针负责遍历,中间的指针负责根据target移位。但是只能通过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) {
        ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>> ();
        Arrays.sort(numbers);
        
        if(numbers.length < 3) return res;
        
        for(int left = 0; left < numbers.length; left ++) {
            for(int right = numbers.length - 1; right > left + 2; right --) {
                int i = left + 1, j = right - 1;
                while(i < j) {
                    if(numbers[left] + numbers[right] + numbers[i] + numbers[j] > target) j--;
                    else if(numbers[left] + numbers[right] + numbers[i] + numbers[j] < target) i++;
                    else {
                            ArrayList<Integer> tmp = new ArrayList<Integer>();
                            tmp.add(numbers[left]);
                            tmp.add(numbers[i]);
                            tmp.add(numbers[j]);
                            tmp.add(numbers[right]);
                            res.add(tmp);
                            while(j - 1 > i && numbers[j] == numbers[j - 1])
                                j--;
                            
                            while(i + 1 < j && numbers[i] == numbers[i + 1])
                                i++;
                            
                            i++;j--;
                    }
                }
                while(right - 1 > left + 2 && numbers[right] == numbers[right - 1]) right--;
            }
            while(left + 1 < numbers.length && numbers[left] == numbers[left + 1]) left++;
        }
        return res;
    }
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值