[leedcode 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[] nums, int target) {
        //本题是3Sum的变形,找出最接近target的值
        //跟3Sum唯一的不同是,需要增加一个变量min(注意初始化值),内层每次循环时,需要与它进行比较。保留住距离target最小的值
        //判断重复的逻辑可有可无,因为本题的输出只是需要返回总结果
        Arrays.sort(nums);
        int res=0;
        int min=Integer.MAX_VALUE;
        for(int i=0;i<nums.length;i++){
            int left=i+1;
           // if(i>0&&nums[i]==nums[i-1])continue;
            int right=nums.length-1;
            while(left<right){
                int temp=nums[i]+nums[left]+nums[right];//注意局部变量的声明,为后面的循环简化表达式
                if(temp==target)
                         return res=target;
                if(temp>target){
                    if(temp-target<min){
                        res=temp;
                        min=res-target;
                    }

                    right--;
                }else{
                     if((target-temp)<min){
                          res=temp;
                          min=target-res;
                     }

                    left++;
                }

            }
        }
        return res;
    }
}
时间: 2024-07-29 04:32:20

[leedcode 16] 3Sum Closest的相关文章

Leetcode 16. 3Sum Closest(指针搜索)

16. 3Sum Closest Medium 131696FavoriteShare Given an array nums of n integers and an integer target, find three integers in nums such that the sum is closest to target. Return the sum of the three integers. You may assume that each input would have e

[LeetCode][Python]16: 3Sum Closest

# -*- coding: utf8 -*-'''__author__ = '[email protected]' 16: 3Sum Closesthttps://oj.leetcode.com/problems/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

leetCode 16.3Sum Closest (离给定值最近的三数之和) 解题思路和方法

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 arr

Java [leetcode 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 =

【LeetCode】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

Array + two points leetcode.16 - 3Sum Closest

题面 Given an array nums of n integers and an integer target, find three integers in nums such that the sum is closest to target. Return the sum of the three integers. You may assume that each input would have exactly one solution. 给定数组,找出并返回最接近target的

Leetcode Array 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

LeetCode #16 3Sum Closest (M)

[Problem] 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

LeetCode 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