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. Hololens开发笔记之Gesture手势识别(单击,双击)

    本文使用手势识别实现识别单击及双击手势的功能,当单击Cube时改变颜色为蓝色,当双击Cube时改变颜色为绿色. 手势识别是HoloLens交互的重要输入方法之一.HoloLens提供了底层API和高层 ...

  2. 使用 Sublime、WebStorm 开发 Jade

    Sublime.WebStorm (PhpStorm) 是前端开发者的得力工具,开发 Jade 也不例外. 在配置这些软件的 Jade 开发环境前,请先在系统中安装 Node.js 和 Jade : ...

  3. 3. sort命令

    转自:http://www.cnblogs.com/51linux/archive/2012/05/23/2515299.html sort是在Linux里非常常用的一个命令,管排序的,集中精力,五分 ...

  4. WANem2.3

    http://downloads.sourceforge.net/wanem http://openmaniak.com/wanem_network.php 只能以iso方式运行,安装到硬盘后无法保存 ...

  5. POJ 3142 The Balance

    Description Ms. Iyo Kiffa-Australis has a balance and only two kinds of weights to measure a dose of ...

  6. java变量命名规则

    1.      变量必须以字母,下划线”_”或”$”符号开 2.      变量可以包括数字,但不能以数字开 3.      除了下划线”_”和”$”符号以外,变量名不能包含任何特殊字符 4.     ...

  7. JS动态获取数据

    JS访问数据,有实时获取数据的时候,请加上时间戳 如:'&stampflag=' + Math.round(new Date().getTime() / 1000); 因为有的浏览器(如IE9 ...

  8. css之让文字在一定范围内显示,不超过固定的宽度和高度

  9. 随机打乱工具sklearn.utils.shuffle,将原有的序列打乱,返回一个全新的错乱顺序的值

    Shuffle arrays or sparse matrices in a consistent way This is a convenience alias to resample(*array ...

  10. GIM企业即时通讯

    GIM企业即时通讯是笔者Garfield(QQ:3674571)采用.NetFramework4.0+SQL2008R2开发的一套企业内网/外网 通用的即时通讯(IM)软件,分为服务器端和客户端,通讯 ...