16. 3Sum Closest

来源:互联网 发布:恋爱循环动作数据 编辑:程序博客网 时间:2024/05/16 07:01

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).
Solution 1 Main idea

public class Solution {    public int threeSumClosest(int[] num, int target) {        int result = num[0] + num[1] + num[num.length - 1];        Arrays.sort(num);        for (int i = 0; i < num.length - 2; i++) {            int start = i + 1, end = num.length - 1;            while (start < end) {                int sum = num[i] + num[start] + num[end];                if (sum > target) {                    end--;                } else {                    start++;                }                if (Math.abs(sum - target) < Math.abs(result - target)) {                    result = sum;                }            }        }        return result;    }}


Solution 2 Optimization on solution 1

public class Solution {    public int threeSumClosest(int[] nums, int target) {        Arrays.sort(nums);        int sum = nums[0] + nums[1] + nums[nums.length - 1];        int closestSum = sum;            for(int i = 0; i < nums.length - 2; i++){            if(i==0 || nums[i]!=nums[i-1]){                int left = i + 1, right = nums.length - 1;                while(left < right){                    sum = nums[left] + nums[right] + nums[i];                    if(sum == target){                        return sum;                    }                    if(sum < target){                        //move closer to target sum.                        while(left<right && nums[left] == nums[left+1]){                            left++;                        }                        left++;                    }else if(sum > target){                        //move closer to target sum.                        while(left<right && nums[right] == nums[right-1]){                            right--;                        }                        right--;                    }else{                        return sum;                    }                    //update the closest sum if needed.                    if(Math.abs(target - sum) < Math.abs(target - closestSum)){                        closestSum = sum;                    }                }            }            }        return closestSum;    }}

Solution 3

//More optimization//No need compare each sum ,just to compare possible sum ,so can save time.public int threeSumClosest4(int[] nums, int target) {    Arrays.sort(nums);    int closest=nums[0]+nums[1]+nums[2];    int low,high;    for(int i=0;i<nums.length-1;i++){        low=i+1;        high=nums.length-1;        while(low<high){            if(nums[low]+nums[high]==target-nums[i]) return target;            else if(nums[low]+nums[high]>target-nums[i]){                while(low<high&&nums[low]+nums[high]>target-nums[i]) high--;                if(Math.abs(nums[i]+nums[low]+nums[high+1]-target)<Math.abs(closest-target))                    closest=nums[i]+nums[low]+nums[high+1];            }            else{                while(low<high&&nums[low]+nums[high]<target-nums[i]) low++;                if(Math.abs(nums[i]+nums[low-1]+nums[high]-target)<Math.abs(closest-target))                    closest=nums[i]+nums[low-1]+nums[high];            }        }    }    return closest;}


0 0