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. A计划 hdu2102(bfs一般题)

    A计划 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submis ...

  2. 线程基础的一些理解(一)(java)

     一.多线程的基本概念 线程是指进程中的一个执行场景,也就是执行流程,所以我们首先要聊一聊进程,以及进程和线程的关系 1.什么是进程? 一个进程对应一个应用程序,就像我们在windows系统中启动Wo ...

  3. hadoop学习之hdfs文件系统

    一.hdfs的概念 Hadoop 实现了一个分布式文件系统(Hadoop Distributed File System),简称HDFS. Hadoop是Apache Lucene创始人Doug Cu ...

  4. linux7 安装SVN

    1.安装Linux虚拟机-- 安装后配置a.停止防火墙# systemctl stop firewalld.service# systemctl disable firewalld.service# ...

  5. POJ1269(KB13-D 计算几何)

    Intersecting Lines Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 16681   Accepted: 71 ...

  6. 设计模式(22)--Template Method(模板方法模式)--行为型

    作者QQ:1095737364    QQ群:123300273     欢迎加入! 1.模式定义: 模板方法模式是类的行为模式.准备一个抽象类,将部分逻辑以具体方法以及具体构造函数的形式实现,然后声 ...

  7. 转 Fira Code | 为写程序而生的字体

    原文:Fira Code | 为写程序而生的字体 Fira Code | 为写程序而生的字体 己短不可藏 6月前 · 1199 人阅读 关注TA 程序员福利!!!今天为大家带来一个专为程序员写程序设计 ...

  8. ionic左滑删除

    <html ng-app="ionicApp"> <head> <meta charset="utf-8"> <met ...

  9. 从sqlserver数据库中提取年月日并截取出来

    select convert(varchar,datepart(year,getdate()))--年+'-'+convert(varchar,datepart(month,getdate()))-- ...

  10. load file within a jar

    String examplejsPrefix = "example"; String examplejsSuffix = "js"; String exampl ...