题目

给定一个数组,和一个指定的值,找出数组中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. SQL Server数据导入导出的几种方法

    在涉及到SQL Server编程或是管理时一定会用到数据的导入与导出, 导入导出的方法有多种,结合我在做项目时的经历做一下汇总: 1. SQL Server导入导出向导,这种方式是最方便的. 导入向导 ...

  2. JavaScript基础精华03(String对象,Array对象,循环遍历数组,JS中的Dictionary,Array的简化声明)

    String对象(*) length属性:获取字符串的字符个数.(无论中文字符还是英文字符都算1个字符.) charAt(index)方法:获取指定索引位置的字符.(索引从0开始) indexOf(‘ ...

  3. pancake sort的几个问题

    1. 每次找剩下序列中的最大值,可以进行pancake sort,时间复杂度是O(n^2): 2. 求最少交换次数进行pancake sort的问题是个NP问题,搜索的时候,upper bound是2 ...

  4. (转)Struts 拦截器

    一.拦截器是怎么实现: 实际上它是用Java中的动态代理来实现的 二.拦截器在Struts2中的应用 对于Struts2框架而言,正是大量的内置拦截器完成了大部分操作.像params拦截器将http请 ...

  5. [转]useradd 与adduser的区别

    转自:Deit_Aaron的专栏 添加用户:useradd -m 用户名  然后设置密码  passwd 用户名 删除用户:userdel  -r  用户名 1. 在root权限下,useradd只是 ...

  6. 修改tabbarcontroller选中图片及选中颜色

    1.修改选中图片: UITabBarItem* item = [self.tabBarController.tabBar.items objectAtIndex:1];   //从0开始 item.s ...

  7. iOS开原项目

    http://www.lanrenios.com/ios/project/2013/0729/1433.html http://www.cnblogs.com/xiaobaizhu/archive/2 ...

  8. DirectX 3D 之C#开发

    C#下进行directX的3D开发,一个旋转的4棱锥的例子. 建议看两个文档<Managed DirectX 9图形和游戏编程简略中文文档>和<Managed DirectX 9 S ...

  9. C# WebBrowser准确判断网页最终装载完毕

    == 最近写了个软件叫WebAutoScript,目的用于,网页的自动操作处理,就是说,所有你在网页上面的操作,都可以录到一个脚本中,然后可以回放这个操作过程..我是说任何过程. 程序是用C#写的,其 ...

  10. HDU 1754 I Hate It (线段树 单点更新)

    题目链接 中文题意,与上题类似. #include <iostream> #include <cstdio> #include <cstring> #include ...