15. 3Sum && 16. 3Sum Closest

15. 3Sum

Given an array S of n integers, are there elements abc in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.

Note: The solution set must not contain duplicate triplets.

For example, given array S = [-1, 0, 1, 2, -1, -4],

A solution set is:
[
  [-1, 0, 1],
  [-1, -1, 2]
]
 
 
public class Solution {
    public List<List<Integer>> threeSum(int[] num) {
        List<List<Integer>> result = new ArrayList<List<Integer>>();
        if(num == null || num.length<3)
            return result;
        
        Arrays.sort(num);
        for(int first = 0;first<num.length-2;++first)
        {
            if(first > 0 && num[first]==num[first-1])
                continue;
            if(num[first]>0)
                break;
            
            int second = first+1;
            int third = num.length -1;
            
            while(second<third)
            {
                int sum = num[first]+num[second]+num[third]; 
                if(sum == 0)
                {
                    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>0)
                    while(second<--third && num[third]==num[third+1]);
                else 
                    while(++second<third && num[second]==num[second-1]);
            }
            
        }
        
        return result;
    }
}

 

16. 3Sum Closest

Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution.
    For example, given array S = {-1 2 1 -4}, and target = 1.

    The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).

 

 
public class Solution {
    public int threeSumClosest(int[] num, int target) {
        if(num == null || num.length<3)
            return 0;
        
        Arrays.sort(num);
        int closest = num[0]+num[1]+num[2];
        for(int first = 0;first<num.length-2;++first) {
            if(first > 0 && num[first]==num[first-1])
                continue;
            int second = first+1;
            int third = num.length -1;
            while(second<third) {
                int sum = num[first]+num[second]+num[third]; 
                if(sum == target)
                    return target; 
                if(sum>target)
                    --third;
                else 
                    ++second;
                if(Math.abs(closest-target)>Math.abs(sum-target))
                   closest = sum;
            }
        }
        return closest;
    }
}

 

 

 
posted @ 2016-03-11 00:06  新一代的天皇巨星  阅读(183)  评论(0)    收藏  举报