题目描述

给出含有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. mac操作liunx

    mkdir demo //创建一个文件夹 touch index.html // 创建一个html文件 rm rouch index.html //删除找个index.html文件 rmdir dem ...

  2. 从零开始针对 .NET 应用的 DevOps 运营实践 - 运行环境搭建

    一.Overview 最近的一段时间,在公司里我都在进行基于 Jenkins 和 SonarQube 配合已有的 Gitlab 搭建部门的持续集成环境的工作,虽然之前有使用过 GitHub Actio ...

  3. 唯品会Java开发手册》1.0.2版阅读

    <唯品会Java开发手册>1.0.2版阅读 1. 概述 <阿里巴巴Java开发手册>,是首个对外公布的企业级Java开发手册,对整个业界都有重要的意义. 我们结合唯品会的内部经 ...

  4. BeetleX之webapi自定义响应内容

    输出内容多样性在webapi服务中比较普遍的,有的情况使用json,xml,图片和二进制流下载等等:为了适应用不同情况的需要,组件支持自定义内容输出.接下来的主要描述组件在webapi如何定义各种内容 ...

  5. spring-boot-route(十四)整合Kafka

    在上一章中SpringBoot整合RabbitMQ,已经详细介绍了消息队列的作用,这一种我们直接来学习SpringBoot如何整合kafka发送消息. kafka简介 kafka是用Scala和Jav ...

  6. Cesium.Viewer

    <!DOCTYPE html><html lang="en"><head> <meta charset="utf-8" ...

  7. intelliJ 软件项目打开运行

    1.导入项目 2.首先更改数据库,找到application-dev.yml文件,更改数据源 3.配置tomcat端口  找到application.yml 文件 然后打开pom.xml 更改版本号 ...

  8. CentOS 7系统常见快捷键操作方式

    快捷键操作方式 Linux系统中一些常见的快捷方式,可有效提高操作效率,在某些时刻也能避免操作失误带来的问题. 最有用的快捷键 序号 快捷键 官方说明 掌握程度 01 Tab 命令或路径等的补全键 移 ...

  9. 我是先学C语言还是先学C++,实不相瞒,鱼和熊掌可兼得!

    这是最近一周时间几个读者小伙伴所提的问题,我顺手截了两个图. 实不相瞒,这类问题之前也经常看到.     每次遇到这种问题,看起来很简单,但是打字一时半会还真说不清,想想今天周末了,写一篇文章来统一聊 ...

  10. laravel job 队列

    1.数据库建表 php artisan queue:table<span> </span>//队列任务表 php artisan queue:failed-table<s ...