leetcode16~3Sum Closest

来源:互联网 发布:远程会诊软件 编辑:程序博客网 时间:2024/06/06 07:18

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 ThreeSumClosest {    public int threeSumClosest(int[] nums, int target) {        if(nums==null || nums.length<3) return 0;        //先排序        Arrays.sort(nums);        int closest = Integer.MAX_VALUE;        int res = 0;        for(int i=0;i<nums.length-2;i++) {            if(i>1 && nums[i]==nums[i-1]) continue;            //固定nums[i]            int top = i+1,tail=nums.length-1;            while(top<tail) {                int sum = nums[i]+nums[top]+nums[tail];                if(sum<target) {                    if(target-sum<closest) {                        //更新最小的差值                        closest = target-sum;                        res = sum;                    }                    top++;                } else if(sum>target) {                    if(sum-target<closest) {                        closest = sum-target;                        res = sum;                    }                    tail--;                } else { //相等,则直接返回                    return sum;                }            }        }        return res;    }}
0 0