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. JSP前后台数据交互

    1.一般方法:在页面加载时载入数据 首先,新建一个包,在包内创建一个类,类实现接口Servlet-javax.servlet 然后,在web.xml文件里配置如下 <?xml version=& ...

  2. Centos7安装Python3的方法

    由于centos7原本就安装了Python2,而且这个Python2不能被删除,因为有很多系统命令,比如yum都要用到. [root@VM_105_217_centos Python-]# pytho ...

  3. LeetCode 531. Longly Pixel I (孤独的像素之一) $

    Given a picture consisting of black and white pixels, find the number of black lonely pixels. The pi ...

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

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

  5. ES6新特性 Class的实现

    ES5之前类的继承是靠原型实现的,而这一过程的实现又涉及到一大堆的原型定义,特别是ES5推出了Object.definePorperty()方法后,代码更加晦涩.但是这种方式正是javascript这 ...

  6. java语言在某个数组中查找某个字符出现的次数

    package com.llh.demo; import java.util.Scanner; /** * * @author llh * */ public class Test { /* * 在某 ...

  7. Windows环境下多线程编程原理与应用读书笔记(8)————信号量及其应用

    <一>线程间同步原因 线程间竞争共享资源: 线程间为完成某个任务而协作: 通过互斥量可以实现线程间由于竞争所需要的同步,通过事件可以实现线程间由于协作所需要的同步. 信号量很好地将互斥量和 ...

  8. Problem J

    Problem Description 有一楼梯共M级,刚开始时你在第一级,若每次只能跨上一级或二级,要走上第M级,共有多少种走法? Input 输入数据首先包含一个整数N,表示测试实例的个数,然后是 ...

  9. windy数(数位DP)

    windy数Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:165888KB     64bit I ...

  10. addEventListener 的事件函数的传递【转载】

    addEventListener 参数如下: addEventListener(type, listener[, useCapture]); type,事件名称 listener,事件处理器 useC ...