Given an array nums of n integers and an integer target, find three integers in nums such that the sum is closest to target. Return the sum of the three integers. You may assume that each input would have exactly one solution.

Example:

Given array nums = [-1, 2, 1, -4], and target = 1.

The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).
想法:与之前的类似,使用双指针,另外设立一个变量去保存最接近target的值
class Solution {
public:
    int threeSumClosest(vector<int>& nums, int target) {
        int len = nums.size();
         > len)
            ;
        ) + nums.at() + nums.at();
        sort(nums.begin(),nums.end());
         ; i < len- ; i++){
             && nums[i] == nums[i-])
                continue;
            ;
            ;
            while(left < right){
                int sum = nums.at(i) + nums.at(left) + nums.at(right);
                if(sum == target)
                    return sum;
                if(abs(sum - target) < abs(temp - target)){
                    temp = sum;
                }
                if(sum < target){
                    left++;
                }else{
                    right--;
                }
            }
        }
        return temp;
    }
};

leetcode16—3 Sum Closet的更多相关文章

  1. LeetCode_3 sum closet

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

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

  3. LeetCode数组解题模板

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

  4. leetcode 之Sum系列(七)

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

  5. LeetCode - Two Sum

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

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

  7. Leetcode 笔记 112 - Path Sum

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

  8. POJ 2739. Sum of Consecutive Prime Numbers

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

  9. BZOJ 3944 Sum

    题目链接:Sum 嗯--不要在意--我发这篇博客只是为了保存一下杜教筛的板子的-- 你说你不会杜教筛?有一篇博客写的很好,看完应该就会了-- 这道题就是杜教筛板子题,也没什么好讲的-- 下面贴代码(不 ...

随机推荐

  1. MySQL,Oracle建立主键自增表

    MySQL 在建表的时候声明字段即可 id int auto_increment primary key not null Oracle 第一步:建立表 drop table t_role; crea ...

  2. 【JavaFx教程】第一部分:Scene Builder

    第一部分的主题 开始了解 JavaFX . 创建并运行一个 JavaFX 项目. 使用 Scene Builder 来设计用户界面. 使用 模型 - 视图 - 控制器(MVC)模式 构造基础的应用. ...

  3. Java虚拟机 - 结构原理与运行时数据区域

    http://liuwangshu.cn/java/jvm/1-runtime-data-area.html 前言 本来计划要写Android内存优化的,觉得有必要在此之前介绍一下Java虚拟机的相关 ...

  4. PHP trick(代码审计关注点)

    随着代码安全的普及,越来越多的开发人员知道了如何防御sqli.xss等与语言无关的漏洞,但是对于和开发语言本身相关的一些漏洞和缺陷却知之甚少,于是这些点也就是我们在Code audit的时候的重点关注 ...

  5. HDU4960(SummerTrainingDay03-F dp)

    Another OCD Patient Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Ot ...

  6. jQuery中.bind() .live() .delegate() .on()区别

    $(selector).bind(event,data,function) $(selector).live(event,data,function)//jquery1.9版本以下支持,jquery1 ...

  7. IDEA项目搭建四——使用Mybatis实现Dao层

    一.引入mybatis及mysql的jar包 可以从阿里云上面查找版本,db操作放在dao层所以打开该层的pom.xml文件,找到<dependencies>节点增加两个引入 <de ...

  8. [我的阿里云服务器] —— WordPress Permalink Settings

    前言: 固定链接(Permalink)是博客日志.分类及其他博客内容列表的永久URL. 别人可以通过固定链接链接到你的文章上,你也可以在email中发送某篇日志的链接. 所有日志的URL应为永久性.固 ...

  9. Visual Studio 2012 Update 1 离线升级包(相当于VS2012 SP1离线补丁包)

    Visual Studio 2012 Update 1 发布也有一段时间了,吾乐吧尝试了好几次在线升级,但是网络不给力啊,结果都失败了.于是一直都想找到官方提供的VS2012 SP1完整离线升级包,不 ...

  10. Django开发笔记(一)

    Django开发笔记(一) 标签(空格分隔): Django Python 1. 创建并运行Django项目 创建开发环境 安装Django pip install django==version 执 ...