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).
附上自己的解题

首先我们要有个大概的思路 第一步无疑是判断是否为空和满足三个数的条件

接下来我们对数据进行排序

从数组最小的数字开始进行for循环 将它与它右边第一个数left和数组的最后一个数right相加 获得一个总值 这个总值如果比目标值小将left加一 反之将right减一 如果正好等于 目标值 那么将结果直接返回 循环要维护一个closet值 这个值代表已知的SUM里面最接近target的sum值 循环结束 返回closet;

之所以把初始值设置为integer.max_value/2 是为了 closet-target这个操作不溢出

这段代码耗时22ms 附上细节

显然这段代码不是最优的 进去discuss看看别人的解法

有一段java 3/4ms的解法 果断进去看

public int threeSumClosest(int[] nums, int target) {
Arrays.sort(nums);
int n = nums.length, p = -1; // for getting closest sum3[p] to target
int[] sum3 = new int[n-2];
for (int i=0; i<n-2; i++) {
sum3[i] = nums[i]+nums[i+1]+nums[i+2];
if (sum3[i]==target) return target;
if (sum3[i]<target) p = i;
}
if (p==-1) return sum3[0];
if (p==n-3) return sum3[n-3]; int l, m, r;
if (target-sum3[p] <= sum3[p+1]-target) m = p+1; // (sum3[p]=nums[l]+nums[m]+nums[r])
else m = p+2; // sum3[p+1] is the closest sum
l = m-1;
r = m+1;
int gap = sum3[m-1]-target; // gap records the smallest gap to target for now while (l>=0 && r<=n-1) {
int w = target-nums[l]-nums[r];
int a = l+1, b = r-1;
int tmp = Integer.MAX_VALUE; // tmp records smallest gap for m in (l,r)
while (a<=b) {
int t = nums[m] - w;
if (t==0) return target;
if (t>0) b = m-1;
else a = m+1;
if (Math.abs(t)<Math.abs(tmp)) tmp = t;
m = (a+b)/2;
}
if (tmp>0) l--; // for nums[l]+nums[m]+nums[r], if smallest gap is positive, we need to make the sum less
else r++;
if (Math.abs(tmp)<Math.abs(gap)) gap = tmp;

之所以这段代码运行时间短 是因为它先做了预处理 不像我们的代码直接循环 并且先找到一个接近下标再进行循环 它先遍历一遍相邻的三个数之和 得到一个p值 如果P值没发生变化 那么整个说明没有找到和比target小的 无疑前三个的和就是答案 直接返回 相反如果P是最后三个数的和的第一个下标  那么说明 最后三个数的和都比target小 最后三个数的和无疑是最接近的 直接返回 便利过程中如果找到了和为target的直接返回结果

												

2016/10/28 很久没更了 leetcode解题 3sumcloset的更多相关文章

  1. 2016/10/28 很久没更了 leetcode解题 3sum

    15. 3Sum Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Fi ...

  2. 2016/10/28 很久没更了 leetcode解题 3sum问题进阶版4sum

    18. 4Sum Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c  ...

  3. 2016 10 28考试 dp 乱搞 树状数组

    2016 10 28 考试 时间 7:50 AM to 11:15 AM 下载链接: 试题 考试包 这次考试对自己的表现非常不满意!! T1看出来是dp题目,但是在考试过程中并没有推出转移方程,考虑了 ...

  4. MIT JOS学习笔记02:kernel 01(2016.10.28)

    未经许可谢绝以任何形式对本文内容进行转载! 在文章开头不得不说的是,因为这部分的代码需要仔细理清的东西太多,所以导致这篇分析显得很啰嗦,还请谅解. 我们在上一篇文章已经分析了Boot Loader的功 ...

  5. 很久没来这里,今天的评测java怪东西,左右Date类和时间戳转换

    在发展过程中,经常会遇到利用上课时间.说话的Date类就不得不提时间戳,左右fr=aladdin" target="_blank">的定义大家能够看看网上对时间戳的 ...

  6. 百度翻译api初使用(很久没写python了,写几行玩玩)

    调用free api做做简易的翻译 这个是百度翻译api文档 http://api.fanyi.baidu.com/api/trans/product/apidoc 照着百度api给的文档向web服务 ...

  7. 久未更 ~ 一之 —— 关于ToolBar

    很久没更博客了,索性开一个久未更 系列 > > > > > 久未更 系列一:关于ToolBar的使用(后续补充) //让 ToolBar 单独使用深色主题 使得 tool ...

  8. My latest news (--2016.10)

    2016.10.31 22:44 一个“程序”,打代码占40%.思考占60% 2016.10.30 20:53 周末,话说今天有晚上讲座,还点名,了,悲催.之前学习的Qt有点问题,悲催.推荐个博文:h ...

  9. 2016.10.5初中部上午NOIP普及组比赛总结

    2016.10.5初中部上午NOIP普及组比赛总结 这次的题目出得挺有质量的.但我觉得我更应该努力了. 进度: 比赛:0+20+0+0=20 改题:AC+AC+AC+AC=AK kk的作业 这题我错得 ...

随机推荐

  1. TMS320C54x系列DSP指令和编程指南——第1章 汇编语言工具概述

    第1章 汇编语言工具概述 TMS320C54x DSP的汇编语言开发工具包括: ■  Assembler      ■  Archiver      ■  Linker      ■  Absolut ...

  2. css3++js钟表

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. 安装Yii框架时init.bat闪退的处理方法

    已经开启了php_openssl扩展还是会闪退 1.右击'计算机'-'属性'-'高级系统属性'-'环境变量(最下边)': 2.在'系统变量'里找到'path',双击,出现'编辑系统变量',在'变量值' ...

  4. ECSHOP通过改变模板路径制作手机站

    ECSHOP通过改变模板路径制作手机站 前提:不使用ECSHOP自带的mobile目录程序来制作手机站. 目的:手机站做成自动识别,通过改变模板路径来显示PC站或手机站. 待续

  5. KT vs SKT [20160816]

    KT:索尔 SKT:茂凯,塔里克,卡西奥佩娅 普朗克+烬,大招开团. 塔里克保护,眩晕.

  6. oracle工作经验(左右连接、decode)

    oracle左右连接:select a.studentno, a.studentname, b.classname from students a, classes b where a.classid ...

  7. entityframework 入门-来自微软

    必备条件 要完成本演练,需要安装 Visual Studio 2010 或 Visual Studio 2012. 如果使用的是 Visual Studio 2010,还需要安装 NuGet. 1.创 ...

  8. 百度地图API示例之设置地图显示范围

    代码 <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" cont ...

  9. angular 的ng-view,ngrouter

    通过ng-view和ngRouter控制页面显示内容: html: <body ng-app="AngularStore"> <div class="c ...

  10. javascript的replace+正则 实现ES6的字符串模版

    采用拼接字符串的形式,将 JSON 数据嵌入 HTML 中.开始时代码量较少,暂时还可以接受.但当页面结构复杂起来后,其弱点开始变得无法忍受起来: 书写不连贯.每写一个变量就要断一下,插入一个 + 和 ...