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

解法:

  这道题让我们求最接近给定值的三数之和,是在之前那道3Sum的基础上又增加了些许难度,那么这道题让我们返回这个最接近于给定值的值,即我们要保证当前三数和跟给定值之间的差的绝对值最小,所以我们需要定义一个变量diff用来记录差的绝对值,然后我们还是要先将数组排个序,然后开始遍历数组,思路跟那道三数之和很相似,都是先确定一个数,然后用两个指针left和right来滑动寻找另外两个数,每确定两个数,我们求出此三数之和,然后算和target的差的绝对值,如果比diff小则更新diff的值。注意,如果三数之和正好等于target,直接返回target即可,因为不会有其他数比本身更接近自己了。代码如下:

public class Solution {
public int threeSumClosest(int[] nums, int target) {
int diff = nums[0] + nums[1] + nums[2] - target; Arrays.sort(nums); for (int i = 0; i < nums.length - 2; i++) {
if (i > 0 && nums[i] == nums[i - 1]) {
continue;
} int left = i + 1, right = nums.length - 1;
while (left < right) {
int sum = nums[i] + nums[left] + nums[right];
diff = Math.abs(sum - target) < Math.abs(diff) ? sum - target : diff; if (sum == target) {
return sum;
} else if (sum < target) {
while (left < right && nums[left++] == nums[left]) {}
} else {
while (left < right && nums[right--] == nums[right]) {}
}
}
}
return target + diff;
}
}

[LeetCode] 16. 3Sum Closest ☆☆☆的更多相关文章

  1. LeetCode 16. 3Sum Closest(最接近的三数之和)

    LeetCode 16. 3Sum Closest(最接近的三数之和)

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

    16. 3Sum Closest Medium 131696FavoriteShare Given an array nums of n integers and an integer target, ...

  3. [LeetCode] 16. 3Sum Closest 最近三数之和

    Given an array nums of n integers and an integer target, find three integers in nums such that the s ...

  4. 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 num ...

  5. 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 giv ...

  6. [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 num ...

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

  8. LeetCode——16. 3Sum Closest

    一.题目链接:https://leetcode.com/problems/3sum-closest/ 二.题目大意: 给定一个数组A和一个目标值target,要求从数组A中找出3个数来,使得这三个数的 ...

  9. 蜗牛慢慢爬 LeetCode 16. 3Sum Closest [Difficulty: Medium]

    题目 Given an array S of n integers, find three integers in S such that the sum is closest to a given ...

随机推荐

  1. 软工1816 · Alpha冲刺(1/10)

    团队信息 队名:爸爸饿了 组长博客:here 作业博客:here 组员情况 组员1(组长):王彬 过去两天完成了哪些任务 前后端代码规范统一 针对之前的alpha冲刺安排进一步细化任务卡片 明确apl ...

  2. 对编码内容多次UrlDecode

    对编码内容多次UrlDecode,并不会影响最终结果. 尝试阅读了微软的源代码,不过不容易读懂. 网址:https://referencesource.microsoft.com/#System/ne ...

  3. .mat转成.npy文件+Python(Pytorch)压缩裁剪图片

    需求:现有数据文件V1.mat,里面包含多个数据集,现需将里面的images数据集提取出来,然后进行压缩裁剪成指定大小 V1.mat数据集目录: 1.从mat文件中提取数据(使用Python) V1. ...

  4. 使用WCF上传数据

    通过传递Stream对象来传递大数据文件,但是有一些限制: 1.只有 BasicHttpBinding.NetTcpBinding 和 NetNamedPipeBinding 支持传送流数据. 2. ...

  5. SQL SERVER技术内幕之7 透视与逆透视

    1.透视转换 透视数据(pivoting)是一种把数据从行的状态旋转为列的状态的处理,在这个过程中可能须要对值进行聚合. 每个透视转换将涉及三个逻辑处理阶段,每个阶段都有相关的元素:分组阶段处理相关的 ...

  6. arp获取

    getarp.c /* getarp.c -- This simple program uses an IOCTL socket call to read an entry */ /* from th ...

  7. [OS] 操作系统错题集

    1. (判断) 答案:错 缓冲区有两块:高速缓存区(物理存在)和磁盘缓存区(逻辑存在,实际是内存一块),都不在外存(硬盘). 2. 操作系统的功能:处理机管理(进程管理).作业管理.存储管理.设备管理 ...

  8. matlab eval【转】

    Matlab 简单谈谈EVAL函数的用法 EVAL(s)相当于把字符串s的内容作为语句来执行. 比如:eval('a=3*5') 和直接在command 窗口中输入 a=3*5 等效 eval 一个经 ...

  9. hash 默认使用equal进行元素比较 防止元素重复

    hash 默认使用equal进行元素比较 防止元素重复

  10. html dom与javascript的关系 -我们用JavaScript对网页(HTML)进行的所有操作都是通过DOM进行的

    一,什么是DOM (参考源http://www.cnblogs.com/chaogex/p/3959723.html) DOM是什么 DOM全称为The Document Object Model,应 ...