问题叙述性说明:

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

基本思路:

本题能够利用上一篇《3 Sum》同样的思路来处理。就是对数组排序,然后利用数字之间的序关系降低对不必要情况的处理。

代码:

int threeSumClosest(vector<int> &num, int target)   //C++
{
//You may assume that each input would have exactly one solution.
int sum = num[0]+num[1]+num[2];
int dif = abs(sum -target); sort(num.begin(), num.end());
for (int i = 0; i < num.size() - 2;)
{
int l = i + 1, r = num.size() - 1;
while (l < r)
{
int tmpdif = num[l] + num[r] + num[i] - target;
if ( tmpdif <= -dif) l++;
else if (tmpdif > -dif && tmpdif < dif)
{
dif = abs(tmpdif);
sum = num[l] + num[r] + num[i];
if(tmpdif < 0)
do { l++; }while (l < r && num[l - 1] == num[l]);
else if(tmpdif > 0)
do { r--; }while (l < r && num[r + 1] == num[r]);
else return target;
}
else r--;
}
do{ i++; }while (i < num.size() - 1 && num[i - 1] == num[i]);
}
return sum;
}

版权声明:本文博客原创文章,博客,未经同意,不得转载。

[leetcode]3 Sum closest的更多相关文章

  1. LeetCode:Path Sum I II

    LeetCode:Path Sum Given a binary tree and a sum, determine if the tree has a root-to-leaf path such ...

  2. Lintcode: Subarray Sum Closest

    Given an integer array, find a subarray with sum closest to zero. Return the indexes of the first nu ...

  3. [leetcode]_K Sum 问题

    问题:K Sum问题是一个问题系列,在一个数组中找K个数的和能够满足题目中要求.从2 Sum 到 3 Sum , 3 Sum Clozet , 4 Sum..解法虽一开始不容易想到,但get到解题技能 ...

  4. Subarray Sum Closest

    Question Given an integer array, find a subarray with sum closest to zero. Return the indexes of the ...

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

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

  6. 剑指offer 65. 不用加减乘除做加法(Leetcode 371. Sum of Two Integers)

    剑指offer 65. 不用加减乘除做加法(Leetcode 371. Sum of Two Integers) https://leetcode.com/problems/sum-of-two-in ...

  7. [LeetCode] Two Sum 两数之和

    Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...

  8. LeetCode (13): 3Sum Closest

    https://leetcode.com/problems/3sum-closest/ [描述] Given an array S of n integers, find three integers ...

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

随机推荐

  1. Qt之界面出现、消失动画效果

    在学习Qt的这2.3个月里,对Qt越发感兴趣,从刚开始的盲目.无所适从到现在的学习.研究.熟练.掌握的过程中,我学到了很多东西,也学会了如何通过自学让自己更加成熟.强大起来,如何更有效地提高自己学习. ...

  2. Delphi下用API代码创建Form

    program PMyWindowClass; uses  Windows,  Messages,  SysUtils; type  TMyWindow = class(TObject)  priva ...

  3. kali之ARP欺骗获取图片流

    其实很简单,就两步: 1. 后接三个参数: 网卡:eth0    网关:10.0.0.1    攻击目标:10.0.0.128 2.启动监听 会弹出一个框 里面就会显示攻击目标通过浏览器访问的页面上的 ...

  4. android文件下载大小和网络不一致(偏大)

    今天在写一个文件下载的程序,在网上搜索了一个抄,用来下载MP3文件. 但是发现下载的MP3文件比原来的文件要大,而且MP3中会有杂音. 在Log中加入日志后发现: 从  网络流中获取的流长度为3000 ...

  5. python获取实时股票信息

    Python3获取股票行情数据(中国个股/中国指数/全球指数) #!/usr/local/bin/python3 #coding=utf-8 #source http://www.cnblogs.co ...

  6. 性能测试之LoadRunner11 破解

    1. 下载破解文件lm70.dll和mlr5lprg.dll     lm70.dll文件,覆盖x:\Program Files\Mercury\LoadRunner\bin下文件即可.     ml ...

  7. Eclipse设置Android Logcat输出字体大小

    Window -> Preferences -> Android -> Logcat -> Display Font:点击"Change"button 如图 ...

  8. 构建基于Jenkins + Github的持续集成环境

    搭建持续集成首先要了解什么是持续集成,带着明确的目标去搭建持续集成环境才能让我们少走很多弯路.持续集成(Continuous integration)简称CI,是一种软件开发的实践,可以让团队在持续集 ...

  9. Opencv实现图像的灰度处理,二值化,阀值选择

    前几天接触了图像的处理,发现用OPencv处理确实比較方便.毕竟是非常多东西都封装好的.可是要研究里面的东西,还是比較麻烦的,首先,你得知道图片处理的一些知识,比方腐蚀,膨胀,仿射,透射等,还有非常多 ...

  10. squid+apache实现缓存加速

    本实例是squid和apache在同一台机器上,squid做前端反向代理.port为80,apache作为后端web,port为81 serverip:172.16.8.102 1.首先介绍下版本号选 ...