首页 > 其他 > 详细

18. 4Sum

时间:2016-03-14 07:05:53      阅读:261      评论:0      收藏:0      [点我收藏+]

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)

 

Subscribe to see which companies asked this question

Hide Similar Problems
 (E) Two Sum (M) 3Sum
 

 

public class Solution {
    public List<List<Integer>> fourSum(int[] nums, int target) {
        List<List<Integer>> results = new ArrayList<List<Integer>>();
        if(nums.length <4)
            return results;
            
        Arrays.sort(nums);
        if(nums[nums.length - 1]+nums[nums.length - 2]+nums[nums.length - 3]+nums[nums.length - 4]<target) 
            return results;
            
        for(int first = 0;first<nums.length-3;++first)
        {
            if(first > 0 && nums[first]==nums[first-1])
                continue;
            if(target < nums[first]+nums[first+1]+nums[first+2]+nums[first+3])
                break;
            if(target>=0 && nums[first]>target)
                break;
            List<List<Integer>> _3SumResults = threeSum(nums, first+1, target-nums[first]);
            if(_3SumResults.size() > 0)
            {
                for(List<Integer> _3SumResult: _3SumResults)
                {
                    List<Integer> result = new ArrayList<Integer>(4);
                    result.add(nums[first]);
                    result.addAll(_3SumResult);
                    results.add(result);
                }
            }
        }
        
        return results;
    }
    
    public List<List<Integer>> threeSum(int[] num, int start, int target) {
        List<List<Integer>> result = new ArrayList<List<Integer>>();
        if(num[num.length - 1]+num[num.length - 2]+num[num.length - 3]<target) 
            return result;
            
        for(int first = start;first<num.length-2;++first)
        {
            if(first > start && num[first]==num[first-1])
                continue;
            if(target<num[start]+num[start+1]+num[start+2])
                break;
            if(target>=0 && num[first]>target)
                break;
            int twoSumTarget = target - num[first];
            int second = first+1;
            int third = num.length -1;
            
            while(second<third)
            {
                int sum = num[second]+num[third]; 
                if(sum == twoSumTarget)
                {
                    List<Integer> l = new ArrayList<Integer>();
                    l.add(num[first]);
                    l.add(num[second]);
                    l.add(num[third]);
                    result.add(l);
                    while(++second<third && num[second]==num[second-1]);
                    while(second<--third && num[third]==num[third+1]);
                }
                else if(sum>twoSumTarget)
                    while(second<--third && num[third]==num[third+1]);
                else 
                    while(++second<third && num[second]==num[second-1]);
            }
            
        }
        
        return result;
    }
}

 

18. 4Sum

原文:http://www.cnblogs.com/neweracoding/p/5274432.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!