题目

给定一个数组,和一个指定的值,找出数组中3个数,它的和最接近这个指定值,并返回这个和。

解法

和上一题找3个数的和为0一样,先排序再遍历,这一次不需要记录路径。

代码

 class Solution {
public:
int threeSumClosest(vector<int> &num, int target) {
int result, min_gap = INT_MAX;
sort(num.begin(), num.end());  //先排序 for(int a = ; a < num.size() - ; ++a)
for(int b = a + , c = num.size() - ; b < c; )
{
int sum = num[a] + num[b] + num[c];
int gap = sum - target; if(gap == )  //找到相等的值,直接返回
return target; if(abs(gap) < min_gap)
{
min_gap = abs(gap);
result = sum;
} if(sum > target)
--c;
else
++b;
}
return result;
}
};

LeetCode题解——3Sum Closest的更多相关文章

  1. LeetCode 16. 3Sum Closest(最接近的三数之和)

    LeetCode 16. 3Sum Closest(最接近的三数之和)

  2. Leetcode 16. 3Sum Closest(指针搜索)

    16. 3Sum Closest Medium 131696FavoriteShare Given an array nums of n integers and an integer target, ...

  3. 【leetcode】3Sum Closest

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

  4. [Leetcode][016] 3Sum Closest (Java)

    题目: https://leetcode.com/problems/3sum-closest/ [标签]Array; Two Pointers [个人分析] 这道题和它的姊妹题 3Sum 非常类似, ...

  5. LeetCode(16)题解--3Sum Closest

    https://leetcode.com/problems/3sum-closest/ 题目: Given an array S of n integers, find three integers ...

  6. LeetCode (13): 3Sum Closest

    https://leetcode.com/problems/3sum-closest/ [描述] Given an array S of n integers, find three integers ...

  7. LeetCode——16. 3Sum Closest

    一.题目链接:https://leetcode.com/problems/3sum-closest/ 二.题目大意: 给定一个数组A和一个目标值target,要求从数组A中找出3个数来,使得这三个数的 ...

  8. [LeetCode] 16. 3Sum Closest 最近三数之和

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

  9. Leetcode 16. 3Sum Closest

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

随机推荐

  1. 利用纯真ip地址库 查询 ip所属地

    1. 首先下周数据源,选择是纯真数据库:http://www.cz88.net/ 2. 安装后,打开软件,将数据导出为txt格式. 3. 处理数据,参照网上的文章(http://www.jb51.ne ...

  2. PowerDesigner模型设计

    原文:PowerDesigner模型设计 绪论 Sybase PowerDesigner(简称PD)是最强大的数据库建模工具,市场占有率第一,功能也确实十分强大,现在最新版本是15.1,已经支持最新的 ...

  3. 88. Merge Sorted Array

    题目: Given two sorted integer arrays A and B, merge B into A as one sorted array. Note:You may assume ...

  4. 68. Text Justification

    题目: Given an array of words and a length L, format the text such that each line has exactly L charac ...

  5. C++:默认的构造函数

    注意:如果类中用户没有定义构造函数,系统会自动提供一个函数体为空的默认构造函数. 但是,只要类中定义了一个构造函数(不一定无参构造函数),系统将不再给它提供 默认的构造函数.因为,默认的构造函数被类中 ...

  6. 转Unity 异常操作

    摘要 使用 unity 处理异常的方法可能会与你的直觉不符.本文将给出正确的处理方法,并简单剖析Unity这部分源代码. 处理异常 打算用Unity的AOP截获未处理的异常,然后写个日志什么的,于是我 ...

  7. JDBC学习总结(二)

    1.JDBC的基本应用 1)创建数据库: create database test;   use test; 2)创建表: create table student(     id int(4) no ...

  8. HDFS 小文件处理——应用程序实现

    在真实环境中,处理日志的时候,会有很多小的碎文件,但是文件总量又是很大.普通的应用程序用来处理已经很麻烦了,或者说处理不了,这个时候需要对小文件进行一些特殊的处理——合并. 在这通过编写java应用程 ...

  9. Android权限安全(7)binder,service,zygote安全相关简介

    binder 提供服务的service中的binder thread 检查调用者的uid 不是root,system就异常. service 也检查调用者的uid 不是root,system,只能注册 ...

  10. Android开发之“点9”

    “点九”是andriod平台的应用软件开发里的一种特殊的图片形式,文件扩展名为:.9.png智能手机中有自动横屏的功能,同一幅界面会在随着手机(或平板电脑)中的方向传感器的参数不同而改变显示的方向,在 ...