问题叙述性说明:

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. HTTP协议结构

    HTTP报文=从客户机到服务器的请求+从服务器到客户机的响应 1.请求报文的格式如下: 请求头   通用信息头    请求头    实体头    报文主体 请求行的格式为: Method[分隔符]Re ...

  2. QT---系统托盘图标不显示原因

    很久没用QT写UI相关的东西了,有些东西都忘记了,今天竟然忘记了系统托盘图标是怎么显示的了.下面说下解决方法 1.现象, 设置了QSystemTrayIcon的Icon图标,但就是不显示自己设置的图片 ...

  3. EasyUI - Draggable 拖动控件

    效果: html代码: <div id="box" style="width: 400px; height: 200px; background-color: #f ...

  4. Linux - 文件系统结构

    文件系统结构:   Linux文件系统为一个倒转的系统单根树状结构. 根为   / 严格区分大小写. 路径使用   /    分割,Windows使用  \     . 当前工作目录: 每一个Shel ...

  5. 如何在一个jpg图片上面叠加文字

    1.将jpg转为bmp格式 2.在bmp文件上写上所需文字 3.将写入文字的bmp文件重新转为jpg格式 http://dev.csdn.net/develop/article/22/22948.sh ...

  6. Perl BEGIN块和END块

    [root@wx03 5]# cat a5.pl END {print cccccccccccc."\n"}; print "aaaaaaaaaaaaa\n"; ...

  7. 7个基于Linux命令行的文件下载和网站浏览工具

    7个基于Linux命令行的文件下载和网站浏览工具 时间:2015-06-01 09:36来源:linux.cn 编辑:linux.cn 点击: 2282 次 Linux命令行是GNU/Linux中最神 ...

  8. meta viewport标签的使用说明(手机浏览缩放控制)

    网页中有这么一句时,在iPhone的浏览器中将以原始大小显示,并不允许缩放. <meta name="viewport" content="width=device ...

  9. 流式计算-Jstorm提交Topology过程(上)

    Topology是Jstorm对有向无环图的抽象,内部封装了数据来源spout和数据处理单元bolt,以及spout和bolt.bolt和bolt之间的关系.它能够被提交到Jstorm集群. 本文以J ...

  10. jar包生制作几种方法,jar包导出三种方法:eclipse导出、jar命令、FatJar插件

    Eclipse将引用了第三方jar包的Java项目打包成jar文件的两种方法 方案一:用Eclipse自带的Export功能 步骤1:准备主清单文件 “MANIFEST.MF”, 由于是打包引用了第三 ...