lintcode-medium-3 Sum 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.

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).

思路和3 Sum基本一致,稍微有点变化而已。

public class Solution {
    /**
     * @param numbers: Give an array numbers of n integer
     * @param target : An integer
     * @return : return the sum of the three integers, the sum closest target.
     */
    public int threeSumClosest(int[] numbers ,int target) {
        // write your code here
        if(numbers == null || numbers.length == 0)
            return target;

        Integer result = null;
        Integer diff = null;
        Arrays.sort(numbers);

        for(int i = 0; i < numbers.length - 2; i++){
            if(i > 0 && numbers[i] == numbers[i - 1])
                continue;

            int left = i + 1;
            int right = numbers.length - 1;

            while(left < right){
                if(result == null){
                    result = numbers[i] + numbers[left] + numbers[right];
                    diff = Math.abs(numbers[i] + numbers[left] + numbers[right] - target);
                }

                if(Math.abs(numbers[i] + numbers[left] + numbers[right] - target) < diff){
                    result = numbers[i] + numbers[left] + numbers[right];
                    diff = Math.abs(numbers[i] + numbers[left] + numbers[right] - target);
                }

                if(numbers[i] + numbers[left] + numbers[right] < target){
                    left++;
                    while(left < right && numbers[left] == numbers[left - 1])
                        left++;
                }
                else if(numbers[i] + numbers[left] + numbers[right] > target){
                    right--;
                    while(left < right && numbers[right] == numbers[right + 1])
                        right--;
                }
                else
                    return target;
            }
        }

        return result;
    }
}
时间: 2024-10-13 02:33:22

lintcode-medium-3 Sum Closest的相关文章

[lintcode medium]4 sum

4Sum Given an array S of n integers, are there elements a, b, c, 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

[lintcode medium] Two sum

Two Sum Given an array of integers, find two numbers such that they add up to a specific target number. The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please no

Subarray Sum Closest

Question Given an integer array, find a subarray with sum closest to zero. Return the indexes of the first number and last number. Given [-3, 1, 1, -3, 5], return [0, 2], [1, 3],[1, 1], [2, 2] or [0, 4]. Answer 这道题延续Subarray Sum的思路,即将[0, i]的sum存起来.这里

LintCode-Subarray Sum Closest

Given an integer array, find a subarray with sum closest to zero. Return the indexes of the first number and last number. Example Given [-3, 1, 1, -3, 5], return [0, 2], [1, 3], [1, 1], [2, 2] or [0, 4] Challenge O(nlogn) time Analysis: s[i] = nums[0

【LeetCode-面试算法经典-Java实现】【016-3 Sum Closest(最接近的三个数的和)】

[016-3 Sum Closest(最接近的三个数的和)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 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

lintcode-medium-Subarray Sum Closest

Given an integer array, find a subarray with sum closest to zero. Return the indexes of the first number and last number. Example Given [-3, 1, 1, -3, 5], return [0, 2], [1, 3], [1, 1], [2, 2] or[0, 4]. Challenge O(nlogn) time public class Solution {

3 Sum Closest 解答

Question 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. Example For example, given array S = {-1 2 1 -4}, and target = 1. The sum that is closest

3 Sum 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 = {

[leetcode]3 Sum 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 =