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 1 -4}, and target = 1. The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).
题目标签:Array
这道题目给了我们一个nums array 和一个 target, 让我们找到一组三数之和,并且是最接近于target的。由于我们做过了三数之和,所以差不多是一样方法来做这道题(这里充分说明了,要老老实实顺着题号做下去,较先的题目都可以被用来辅助之后的题)。方法就是先sort一下array,为啥要sort呢,因为要用到two pointers 来遍历找两数之和,只有在从小到大排序之后的结果上,才能根据情况移动left 和right。 当确定好了第一个数字后,就在剩下的array里找两数之和,在加上第一个数字,用这个temp_sum减去target 来得到temp_diff,如果temp_diff比之前的小,那么更新diff 和 closestSum。 利用two pointers 特性, 如果temp_sum 比target 小的话,说明我们需要更大的sum,所以要让left++以便得到更大的sum。 如果temp_sum 比target 大的话,我们就需要更小的sum。如果相等的话,直接return 就可以了。因为都相等了,那么差值就等于0,不会有差值再小的了。
Java Solution:
Runtime beats 86.06%
完成日期:07/12/2017
关键词:Array
关键点:利用twoSum的方法,two pointers 来辅助,每次确定第一个数字,剩下的就是找两个数字之和的问题了
public class Solution
{
public int threeSumClosest(int[] nums, int target)
{
// sort the nums array
Arrays.sort(nums);
int closestSum = 0;
int diff = Integer.MAX_VALUE; // iterate nums array, no need for the last two numbers because we need at least three numbers
for(int i=0; i<nums.length-2; i++)
{
int left = i + 1;
int right = nums.length - 1; // use two pointers to iterate rest array
while(left < right)
{
int temp_sum = nums[i] + nums[left] + nums[right];
int temp_diff = Math.abs(temp_sum - target);
// if find a new closer sum, then update sum and diff
if(temp_diff < diff)
{
closestSum = temp_sum;
diff = temp_diff;
} if(temp_sum < target) // meaning need larger sum
left++;
else if(temp_sum > target) // meaning need smaller sum
right--;
else // meaning temp_sum == target, this is the closestSum
return temp_sum;
}
} return closestSum;
}
}
参考资料:N/A
LeetCode 算法题目列表 - LeetCode Algorithms Questions List
LeetCode 16. 3Sum Closest. (最接近的三数之和)的更多相关文章
- LeetCode 16. 3Sum Closest(最接近的三数之和)
LeetCode 16. 3Sum Closest(最接近的三数之和)
- [leetcode]16. 3Sum Closest最接近的三数之和
Given an array nums of n integers and an integer target, find three integers in nums such that the s ...
- 【LeetCode】16. 3Sum Closest 最接近的三数之和
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:3sum, three sum, 三数之和,题解,lee ...
- Leetcode16.3Sum Closest最接近的三数之和
给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这三个数的和.假定每组输入只存在唯一答案. 例如,给定数 ...
- 016 3Sum Closest 最接近的三数之和
Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...
- LeetCode(16):最接近的三数之和
Medium! 题目描述: 给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这三个数的和.假定每组输入只 ...
- C#LeetCode刷题之#16-最接近的三数之和(3Sum Closest)
目录 问题 示例 分析 问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3620 访问. 给定一个包括 n 个整数的 ...
- LeetCode:最接近的三数之和【16】
LeetCode:最接近的三数之和[16] 题目描述 给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这 ...
- Java实现 LeetCode 16 最接近的三数之和
16. 最接近的三数之和 给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这三个数的和.假定每组输入只存 ...
- Leetcode题库——16.最接近的三数之和
@author: ZZQ @software: PyCharm @file: threeSumClosest.py @time: 2018/10/14 20:28 说明:最接近的三数之和. 给定一个包 ...
随机推荐
- 《Head First Java》读书笔记(1) - Java语言基础
<Head First Java>(点击查看详情) 1.写在前面的话 这本书的知识点说实话感觉有点散乱,但是贵在其将文字转换成了生动和更容易接受的图片,大量的比喻让人感受到了知识点的有趣之 ...
- 深入理解计算机系统(2.5)------C语言中的有符号数和无符号数以及扩展和截断数字
上一篇博客我们讲解了计算机中整数的表示,包括无符号编码和补码编码,以及它们之间的互相转换,个人觉得那是非常重要的知识要点.这篇博客我们将介绍C语言中的有符号数和无符号数以及扩展和截断数字. 1.C语言 ...
- Extjs2.0 desktop 动态创建桌面图标和开始菜单
这几天一直纠结Extjs desktop怎么动态读取数据,用Ext.net已经实现但是不灵活.Ext.net做出来的桌面在窗口关闭后只是隐藏该窗口,并没有释放,对于我这种js菜鸟来说,改那一坨代码要人 ...
- HTML文本
1.HTML元素 2.HTML属性 3.HTML文本格式化 4.HTML样式 1.HTML元素 1.什么是HTML元素 HTML 元素指的是从开始标签(start tag)到结束标签(end tag) ...
- HTML基础入门
1.什么是HTML 2.HTML文件结构 3.HTML文档 4.HTML标签 1.什么是HTML 首先,HTML是一种语言,是用来描述网页的语言 HTML 指的是超文本标记语言 (Hyper Text ...
- Jquery一些常用的方法
整理以前的笔记,在学习JavaScript时候,经常会用到一些方法,但是有时忘掉了具体用法,因此记下.方便以后查阅. 这篇博文先说明这些方法的用途: removeClass().remove().cs ...
- 关于Tomcat一些启动错误的解决方法
一.Eclipse tomcat 启动超时: 错误内容: Server JBoss v4.0 at localhost was unable to start within 50 seconds. I ...
- [UIKit学习]03.关于UILable
代码创建UILabel UILabel *label = [[UILabel alloc] init]; label.text = @"单肩包"; label.frame = CG ...
- 集成Mybatis
本文根据个人喜好记录"腾讯课堂"的<Java项目之Maven+SpringMVC+Spring+Mybatis+MySql消费查询系统>视频教程关键步骤信息,视频地址: ...
- C++格式化输出的好东西
s = FormatFloat("0.######", d); 最多保留6位s = FormatFloat("0.000000", d); 始终保留6位s = ...