问题叙述性说明:

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. axure母版(模板)区域介绍

    axure的模板区域是非常重要的一个功能,网站的头部.尾部部分等很多页面同时用到的内容,都可以使用母版,因为在母版中只需要修改一次,就可以实现所有的页面更新,可以大大的加速原型的制作速度.需要重复理解 ...

  2. 14.18.1 The InnoDB Recovery Process InnoDB 恢复进程:

    14.18.1 The InnoDB Recovery Process InnoDB 恢复进程: InnoDB crash recovery 有几个步骤组成: 1.应用redo log,Redo lo ...

  3. UVA11552------FEWEST FLOPS------区间型的DP

    题目地址:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  4. BZOJ 3110([Zjoi2013]K大数查询-区间第k大[段修改,在线]-树状数组套函数式线段树)

    3110: [Zjoi2013]K大数查询 Time Limit: 20 Sec   Memory Limit: 512 MB Submit: 418   Solved: 235 [ Submit][ ...

  5. 发掘ListBox的潜力(一):自动调整横向滚动条宽度

    <自绘ListBox的两种效果>一文帖出之后,从反馈信息来看,大家对这种小技巧还是很认同.接下来我将继续围绕ListBox写一系列的文章,进一步发掘ListBox的潜力,其中包括:自动调整 ...

  6. uva 12096

    优先队列,主要是STL应用所以复制一下 #include <iostream> #include <cstdio> #include <cstdlib> #incl ...

  7. nodejs+socket.io即时聊天实例

    在这之前你应该先安装好 Node.js,安装过程不再讲解 首先在你的电脑上创建一个新目录,姑且命名为 chat,然后在该目录创建两个文件,分别是 app.js 和 index.html. app.js ...

  8. c++ anonymous namespace -- 匿名空间

    c++ anonymous namespace -- 匿名空间 匿名空间,匿名类,匿名联合体,匿名结构体.   匿名空间   #include <stdio.h> namespace A ...

  9. POJ 2112 Optimal Milking【网络流+二分+最短路】

    求使所有牛都可以被挤牛奶的条件下牛走的最长距离. Floyd求出两两节点之间的最短路,然后二分距离. 构图: 将每一个milking machine与源点连接,边权为最大值m,每个cow与汇点连接,边 ...

  10. OpenJDK1.8.0 源码解析————HashMap的实现(二)

    上一篇文章介绍了HashMap的一部分的知识,算是为下面HashMap的进一步学习做准备吧. 然后写的时候一直在思考的一个问题是,这方面的知识网上的资料也是一抓一大把,即使是这样我为什么还要花费时间去 ...