题目描述

给出含有n个整数的数组s,找出s中和加起来的和最接近给定的目标值的三个整数。返回这三个整数的和。你可以假设每个输入都只有唯一解。

   例如,给定的整数 S = {-1 2 1 -4}, 目标值 = 1.↵↵   最接近目标值的和为 2. (-1 + 2 + 1 = 2).
 

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

示例1

输入

复制

[0,0,0],1

输出

复制

0
链接:https://www.nowcoder.com/questionTerminal/291a866d7c904563876b373b0b157dde?f=discussion
来源:牛客网

class Solution {
public:
    int threeSumClosest(vector<int> &num, int target) {
        int n = num.size();
        int result = num[0] + num[1] + num[n-1];
        sort(num.begin(),num.end());
        for(int i=0;i<n-2;i++)
        {
            int start = i + 1;
            int end = n - 1;
            while(start < end)
            {
                int sum = num[i] + num[start] + num[end];
                if(sum < target)
                    start++;
                else
                    end--;
                 
                if(abs(sum-target) < abs(result-target))
                    result = sum;
            }
        }
        return result;
    }
};


leetcode133:3sum-closest的更多相关文章

  1. LeetCode:3Sum, 3Sum Closest, 4Sum

    3Sum Closest Given an array S of n integers, find three integers in S such that the sum is closest t ...

  2. 6.3Sum && 4Sum [ && K sum ] && 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 a ...

  3. No.016 3Sum Closest

    16. 3Sum Closest Total Accepted: 86565 Total Submissions: 291260 Difficulty: Medium Given an array S ...

  4. 【leetcode】3Sum Closest

    3Sum Closest Given an array S of n integers, find three integers in S such that the sum is closest t ...

  5. 2Sum,3Sum,4Sum,kSum,3Sum Closest系列

    1).2sum 1.题意:找出数组中和为target的所有数对 2.思路:排序数组,然后用两个指针i.j,一前一后,计算两个指针所指内容的和与target的关系,如果小于target,i右移,如果大于 ...

  6. [LeetCode][Python]16: 3Sum Closest

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 16: 3Sum Closesthttps://oj.leetcode.com ...

  7. LeetCode之“散列表”:Two Sum && 3Sum && 3Sum Closest && 4Sum

    1. Two Sum 题目链接 题目要求: Given an array of integers, find two numbers such that they add up to a specif ...

  8. LeetCode 15. 3Sum 16. 3Sum Closest 18. 4Sum

    n数求和,固定n-2个数,最后两个数在连续区间内一左一右根据当前求和与目标值比较移动,如果sum<target,移动较小数,否则,移动较大数 重复数处理: 使i为左至右第一个不重复数:while ...

  9. LeetCode--No.016 3Sum Closest

    16. 3Sum Closest Total Accepted: 86565 Total Submissions: 291260 Difficulty: Medium Given an array S ...

  10. leetcode-algorithms-16 3Sum Closest

    leetcode-algorithms-16 3Sum Closest Given an array nums of n integers and an integer target, find th ...

随机推荐

  1. 【学习笔记/题解】分层图/[JLOI2011]飞行路线

    题目戳我 \(\text{Solution:}\) 关于分层图: 一般用于处理:给你\(k\)次机会对边权进行修改的最短路问题. 算法流程: 建立出\(k\)层图,对应进行\(k\)次操作后的局面. ...

  2. php-fpm 的各种启动方式

    启动 php-fpm 最简单的操作: /usr/local/php/sbin/php-fpm php 5.3.3 以后的php-fpm 不再支持 php-fpm 以前具有的 /usr/local/ph ...

  3. jquery购物车全选,取消全选,计算总金额

    这是html代码 <div class="gwcxqbj"> <div class="gwcxd center"> <div cl ...

  4. linux(centos8):firewalld对于请求会选择哪个zone处理?

    一,firewalld对一个请求会适用哪个zone? 当接收到一个请求时,firewalld具体使用哪个zone? firewalld是通过三个步骤来判断的: source,即:源地址 interfa ...

  5. property和setter装饰器

    # property装饰器 # 作用: 将一个get方法转换为对象的属性. 就是 调用方法改为调用对象 # 使用条件: 必须和属性名一样 # setter方法的装饰器: # 作用:将一个set方法转换 ...

  6. 测试之-Jmeter使用

    一. 修改语言 修改 在 bin 目录下的 Jemeter.properties 二 . Jmeter主要元件 1.测试计划:是使用 JMeter 进行测试的起点,它是其它 JMeter测试元件的容器 ...

  7. python numpy输出排名

    python numpy排序后输出排名 问题: 假设某班的成绩为: 姓名 成绩 名次 小红 95 小黑 67 小白 58 小绿 82 小蓝 76 小橙 79 小可爱 99 请根据表格,输出对应的名次 ...

  8. 51node1256 乘法匿元(扩展欧几里得)

    #include<iostream> using namespace std; int gcd(int a,int b,int &x,int &y){ if (b==0){ ...

  9. 使用creata-react-app脚手架创建react项目时非常慢的问题

    创建react项目必须要有下面两个步骤 cnpm install -g create-react-app  //创建react全局变量 create-react-app my-app //创建一个re ...

  10. HEAP CORRUPTION DETECTED:after Normal block错误方法解决

    一:问题描述: 出现的问题如下: 二:问题产生的原因说明 该问题发生于操作堆内存的时候.产生该问题的原因是:你实际使用的内存大小超出了你实际申请的内存大小,在释放内存的时候就会发生该问题. 举个例子: ...