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. (最接近的三数之和)的更多相关文章

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

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

  2. [leetcode]16. 3Sum Closest最接近的三数之和

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

  3. 【LeetCode】16. 3Sum Closest 最接近的三数之和

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:3sum, three sum, 三数之和,题解,lee ...

  4. Leetcode16.3Sum Closest最接近的三数之和

    给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这三个数的和.假定每组输入只存在唯一答案. 例如,给定数 ...

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

  6. LeetCode(16):最接近的三数之和

    Medium! 题目描述: 给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这三个数的和.假定每组输入只 ...

  7. C#LeetCode刷题之#16-最接近的三数之和(3Sum Closest)

    目录 问题 示例 分析 问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3620 访问. 给定一个包括 n 个整数的 ...

  8. LeetCode:最接近的三数之和【16】

    LeetCode:最接近的三数之和[16] 题目描述 给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这 ...

  9. Java实现 LeetCode 16 最接近的三数之和

    16. 最接近的三数之和 给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这三个数的和.假定每组输入只存 ...

  10. Leetcode题库——16.最接近的三数之和

    @author: ZZQ @software: PyCharm @file: threeSumClosest.py @time: 2018/10/14 20:28 说明:最接近的三数之和. 给定一个包 ...

随机推荐

  1. merge 语法解析

    merge into 支持sqlserver 2008 和以上的版本 无论是INSERT还是UPDATE,从执行之间上看,MERGE INTO(MERGE)都要比直接INSERT/UPDATE的效率高 ...

  2. 用wrk测试nginx/ndoejs/golang

    sudo taskset -c ./wrk -c1 -t1 -d30 http://localhost/hello wrk+nginx(helloworld module) sudo taskset ...

  3. Mysql配置文件my.cnf详细说明

    [表名大小写配置] MySQL在Linux下数据库名.表名.列名.别名大小写规则:  1.数据库名与表名是严格区分大小写  2.表的别名是严格区分大小写  3.列名与列的别名在所有的情况下均是忽略大小 ...

  4. LNMP环境源码搭建

    以前LNMP环境是由运维搭建,自己搭建的时候查找了很多资料,这是我见过的最棒的资料,将过程记录下来分享给大家 为啥使用LNMP而不是LAMP下面来谈谈Nginx的技能 Nginx是一个小巧而高效的Li ...

  5. FastDFS 分布式文件系统的安装与使用

    跟踪服务器:192.168.152.129 (centos1) 存储服务器:192.168.152.130 (centos2) 环境:CentOS 6.6 用户:root 数据目录:/fastdfs ...

  6. 【NOIP】OpenJudge - 15-02:财务管理

    #include<stdio.h>//财务管理 int main() { ]={},sum=,ave=; ;i<=;i++) { scanf("%f",& ...

  7. 不同Activity之间传递线程

    场景:Android由Activiy A创建Activiy B时 ,A创建的线程B中依然需要调用,这时候需要在两个activity之间传递线程的信息. 解决: 方式一:线程自己维护自己的静态句柄(比较 ...

  8. 项目发布Debug和Release版的区别

    一.Debug和Release的区别 Debug:调试版本,包含调试信息,所以容量比Release大很多,并且不进行任何优化(优化会使调试复杂化,因为源代码和生成的指令间关系会更复杂),便于程序员调试 ...

  9. ch3-模板语法({{}} v-html v-bind:id 表达式 指令 修饰符 过滤器)

    1 模板语法 Vue.js 使用了基于 HTML 的模版语法,允许开发者声明式地将 DOM 绑定至底层 Vue 实例的数据. 所有 Vue.js 的模板都是合法的 HTML ,所以能被遵循规范的浏览器 ...

  10. 奥利奥好吃吗?Android 8.0新特性适配测试报告来啦!

    WeTest 导读 谷歌2017 I/O开发者大会上发布了Android 8.0的正式版, 其官方代号为Oreo(奥利奥).网上关于Android8.0新功能特性的介绍已铺天盖地,新功能特性会对程序应 ...