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

此题和之前两个three sum的区别在于,此题是求最接近target的值的三个数的和,和之前three sum一样,利用排序,for循环,然后双指针来解决此题,不同之处在于创建一个sum值,每次更新最小三个数组的和。代码如下:

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px "Helvetica Neue"; color: #454545 }

public class Solution {

public int threeSumClosest(int[] nums, int target) {

Arrays.sort(nums);

int len = nums.length;

int sum = nums[0]+nums[1]+nums[2];

for(int i=0;i<len-2;i++){

int low = i+1,high = len-1;

while(low<high){

int cur = nums[i]+nums[low]+nums[high];

if(cur<=target){

if(Math.abs(sum-target)>Math.abs(cur-target)){

sum = cur;

}

low++;

}else{

if(Math.abs(sum-target)>Math.abs(cur-target)){

sum = cur;

}

high--;

}

}

}

return sum;

}

}

时间: 2024-08-14 07:39:51

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

【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