1. 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.
  2.  
  3. For example, given array S = {- -}, and target = .
  4.  
  5. The sum that is closest to the target is . (- + + = ).

3 sum 的变形,这里不需要考虑重复而已

  1. class Solution {
  2. public:
  3. int threeSumClosest(vector<int> &num, int target) {
  4. // Start typing your C/C++ solution below
  5. // DO NOT write int main() function
  6. sort(num.begin(), num.end());
  7. int len = num.size();
  8. if(len < ) return ;
  9. int res = num[] + num[] + num[];
  10. for(int i = ; i <= len -; ++i){
  11.  
  12. int low = i+;
  13. int high = len -;
  14. while(low < high){
  15.  
  16. int sum = num[i] + num[low] + num[high];
  17. if(sum == target)
  18. return target;
  19. else if(sum < target){
  20. ++low;
  21. if(abs(sum - target) < abs(res - target))
  22. res = sum;
  23. }else{
  24. --high;
  25. if(abs(sum - target) < abs(res - target))
  26. res = sum;
  27.  
  28. }
  29. }
  30. }
  31.  
  32. return res;
  33. }
  34. };

LeetCode_3 sum closet的更多相关文章

  1. LeetCode_3 sum

    Given an array S of n integers, are there elements a, b, c ? Find all unique triplets in the array w ...

  2. leetcode16—3 Sum Closet

    Given an array nums of n integers and an integer target, find three integers in nums such that the s ...

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

  4. LeetCode数组解题模板

    一.模板以及题目分类 1.头尾指针向中间逼近 ; ; while (pos1<pos2) { //判断条件 //pos更改条件 if (nums[pos1]<nums[pos2]) pos ...

  5. leetcode 之Sum系列(七)

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

  6. LeetCode - Two Sum

    Two Sum 題目連結 官網題目說明: 解法: 從給定的一組值內找出第一組兩數相加剛好等於給定的目標值,暴力解很簡單(只會這樣= =),兩個迴圈,只要找到相加的值就跳出. /// <summa ...

  7. Leetcode 笔记 113 - Path Sum II

    题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...

  8. Leetcode 笔记 112 - Path Sum

    题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...

  9. POJ 2739. Sum of Consecutive Prime Numbers

    Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20050 ...

随机推荐

  1. kindle

    http://www.mindmap8.com/Kindle_Paperwhite/20130726266.html http://blog.csdn.net/felomeng/article/det ...

  2. linux下安装apache2.2.27

    1.首先下载httpd-2.2.27.tar.gz用linux命令下载 wget http://mirrors.cnnic.cn/apache//httpd/httpd-2.2.27.tar.gz 2 ...

  3. C#面向对象(二)

    一:抽象方法 1. 在面向对象编程语言中抽象方法指一些只有方法声明,而没有具体方法体的方法.抽象方法一般存在于抽象类或接口中. 在一些父类中,某些行为不是非常明确,因此无法用代码来具体实现,但是类还必 ...

  4. 求职,找工作,平台大PK

    国内 猎聘网:www.lietou.com 拉钩网:Lagou.com 智联招聘:www.zhaopin.com 前程无忧:http://www.51job.com/ 中华英才网:chinahr.co ...

  5. 在C#中实现Socket端口复用

    转载:http://www.csharpwin.com/csharpspace/68.shtml 一.什么是端口复用:        因为在winsock的实现中,对于服务器的绑定是可以多重绑定的,在 ...

  6. C#调用ActiveX控件

    背景:最近项目中需要用到ActiveX控件,项目是在.Net平台下开发的.因此就直接在项目中添加了对ActiveX控件的引用,添加引用成功.在代码中实例化类的实例也没有问题,但在调用其方法或属性时总是 ...

  7. .NET下的加密解密大全(1): 哈希加密

    .NET有丰富的加密解密API库供我们使用,本博文总结了.NET下的Hash散列算法,并制作成简单的DEMO,希望能对大家有所帮助. MD5[csharp]using System; using Sy ...

  8. latch: cache buffers chains故障处理总结

    一大早就接到开发商的电话,说数据库的CPU使用率为100%,应用相应迟缓.急匆匆的赶到现场发现进行了基本的检查后发现是latch: cache buffers chains 作祟,处理过程还算顺利,当 ...

  9. spring源码_下载以及转入eclipse (2016-11-08)

    本例spring源码版本是4.3.0的, 所以jdk需要准备1.8的(不同版本源码要求的jdk不一样) 1.8版本myeclipse10无编译环境,只有运行环境,出现点问题,下载最新版本的Eclips ...

  10. COM简单应用示例

    使用com技术开发模式进行的示例. com技术关键部分源码:主要将所有接口都写入到这个文件中 testinterface.h #ifndef TESTINTERFACE_H #define TESTI ...