题目

给定一个数组,和一个指定的值,找出数组中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. ConcurrentHashMap使用示例

    ConcurrentHashMap使用示例 发表于2年前(2013-07-12 19:05)   阅读(3660) | 评论(0) 25人收藏此文章, 我要收藏 赞5 如何快速提高你的薪资?-实力拍“ ...

  2. Failed to execute goal.....webxml attribute is required...

    maven在打包项目的时候报错 Failed to execute goal org.apache.maven.plugins:maven-war-plugin:2.2:war (default-wa ...

  3. ServletContentLIstener接口演示ServletContext的启动和初始化

    ServletContextListener接口中包含两个方法,一个是contextInitialized()方法, 用来监听ServletContext的启动和初始化:一个是contextDestr ...

  4. 深度神经网络入门教程Deep Neural Networks: A Getting Started Tutorial

    Deep Neural Networks are the more computationally powerful cousins to regular neural networks. Learn ...

  5. 好用的工具之一 ---- Sublime Text

    官网地址和详细解释:http://www.sublimetext.com/ 异次元的一些更详细的个人体验细节:http://www.iplaysoft.com/sublimetext.html

  6. 图形数据库、NOSQL和Neo4j

    简介 在众多不同的数据模型里,关系数据模型自80年代就处于统治地位,而且有不少实现,如Oracle.MySQL和MSSQL,它们也被称为关系数据库管理系统(RDBMS).然而,最近随着关系数据库使用案 ...

  7. Android Studio 我常用快捷键

    0. Ctrl+Alt+L 格式化代码 Ctrl+Alt+O 优化导入的类 1. 重载方法 Ctrl+O 2.Ctrl+shift+Enter:自动匹配相对应的语法结构,比如if,do-while,t ...

  8. fastdfs-client-java 文件上传

    FastDFS是一个开源的轻量级分布式文件系统,它对文件进行管理,功能包括:文件存储.文件同步.文件访问(文件上传.文件下载)等,解决了大容量存储和负载均衡的问题.特别适合以文件为载体的在线服务,如相 ...

  9. Java [Leetcode 102]Binary Tree Level Order Traversal

    题目描述: Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to ...

  10. 调用DirectDraw接口和调DirectDraw7接口的不同点对比

    调用DirectDraw接口步骤: 1.       包含链接库ddraw.lib 2.       初始化窗口类型(全屏独占时类型用popup). 3.       在初始化窗口后初始化Direct ...