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).
class Solution {
public:
int threeSumClosest(vector<int> &num, int target) {
sort(num.begin(),num.end());
int base = ,left = ,right = num.size() - ;
int minSum = num[base] + num[left] + num[right];int mindistance = abs(minSum - target);
for(base = ;base< num.size();base++)
{
left = base + ; right = num.size() -;
while(left < right)
{
int sum = num[base] + num[left] + num[right];
int dis = sum - target ;
if(dis > )
{
right--;
}else if(dis <)
{
left++;
}else
{
return sum;
} if(abs(dis) < mindistance)
{
minSum = sum;
mindistance = abs(dis);
}
} }
return minSum;
}
};

leecode -- 3sum Closet的更多相关文章

  1. Leetcode 3Sum Closet

    二手和3Sum像几乎相同的想法.二进制搜索.关键修剪.但是,在修剪做出很多错误. 然后还有一个更加速了原来的想法O(n^2). #include<iostream> #include &l ...

  2. 16.3Sum Closet

    思路: 暴力,复杂度为 \(O(n^3)\),超时 class Solution { public: int threeSumClosest(vector<int>& nums, ...

  3. LeetCode——3Sum &amp; 3Sum Closest

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

  4. 15. 3Sum、16. 3Sum Closest和18. 4Sum

    15 3sum Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = ...

  5. 《LeetBook》leetcode题解(18) : 4Sum[M]

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

  6. leetcode 之Sum系列(七)

    第一题是Two Sum 同样是用哈希表来做,需要注意的是在查打gap是要排除本身.比如target为4,有一个值为2,gap同样为2. vector<int> twoSum(vector& ...

  7. Leetcode 3Sum Closest

    Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...

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

  9. 算法题思路总结和leecode继续历程

    2018-05-03 刷了牛客网的题目:总结思路(总的思路跟数学一样就是化简和转化) 具体启发点: 1.对数据进行预处理排序的思想:比如8皇后问题 2.对一个数组元素进行比较的操作,如果复杂,可以试试 ...

随机推荐

  1. 异步任务--celery发送邮件

    安装两个python包: pip install celery==3.1.25 pip install django-celery==3.1.17 在配置文件settings.py中做如下配置: a) ...

  2. HTML form表单回车触发提交

    <script type="text/javascript">    function submitByEnter()    {        if(event.key ...

  3. Linux下如何高效删除一个几十G的文本文件的最后一行或几行

    当我们在服务器端记录日志或文本数据时,有时候会有需要删除一个大文件的最后几行,这时如何才能高效实现. 上网浏览终于找到dd命令,亲测如下,删除一个32GB的日志文件最后100行仅需要4分钟 [root ...

  4. System.getProperty参数大全

    System.getProperty()参数大全 #java.version                                    Java Runtime Environment v ...

  5. EF框架搭建小总结--CodeFirst模型优先

    前言:之前在下总结编写了一篇 EF框架搭建小总结--ModelFirst模型优先 博文,看到一段时间内该博文的访问量蹭.蹭蹭.蹭蹭蹭...往上涨(实际也不是很多,嘿嘿),但是还是按捺不住内心的喜悦(蛮 ...

  6. Elasticsearch 索引别名与Template

    在使用elasticsearch的时候,经常会遇到需要淘汰掉历史数据的场景. 为了方便数据淘汰,并使得数据管理更加灵活,我们经常会以时间为粒度建立索引,例如: 每个月建立一个索引:monthly-20 ...

  7. 径向基网络(RBF network)

    来源:http://blog.csdn.net/zouxy09/article/details/13297881 1.径向基函数 径向基函数(Radical Basis Function,RBF)方法 ...

  8. iOS 输入时键盘处理问题

    最正规的办法,用通知 step 1:在进入视图的时候添加监视:(viewDidLoad什么的) //监听键盘的通知 [[NSNotificationCenter defaultCenter] addO ...

  9. Java语言实现机制

    Java语言实现机制 1.Java虚拟机(Java Virtual Machine) Java虚拟机(JVM)是在一台计算机上由软件模拟也可以用硬件来实现的假想的计算机.它定义了指令集(相当于中央处理 ...

  10. Problem L

    Problem Description 在2×n的一个长方形方格中,用一个1× 2的骨牌铺满方格,输入n ,输出铺放方案的总数. 例如n=3时,为2× 3方格,骨牌的铺放方案有三种,如下图: L&qu ...