题目

给定一个数组,和一个指定的值,找出数组中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. 安装xampp后,出现“Apache 2 Test Page powered by CentOS“

    因为是在本地测试,所以没有去考虑为什么会这样,考虑太多的原因.只要能运行就行. 所以网络搜索了一番. 最后,解决办法是: 1, 找到apachectl. 那么就在命令行敲find / -name ap ...

  2. PostMessage与SendMessage各自的问题

     深入解析SendMessage.PostMessage 本文将使用C++语言,在MFC框架的配合下给出PostMessage.SendMessage等的使用方式与使用不当造成的后果(讨论均针对自定义 ...

  3. 33. Search in Rotated Sorted Array

    题目: Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7  ...

  4. RecyclerView(5)官方教程带简单示例

    Create Lists The RecyclerView widget is a more advanced and flexible version of ListView. This widge ...

  5. GetKeyState和GetAsyncKeyState以及GetKeyboardState函数的用法与区别

    GetKeyState.GetAsyncKeyState.GetKeyboardState函数的区别: 1.BOOL GetKeyboardState( PBYTE lpKeyState );获得所有 ...

  6. sendmessage()模拟鼠标点击

    {鼠标软模拟:好处就是不会真的移动鼠标 开始按钮 坐标 x=386y=387 }sendmessage(hookHwnd,messages.WM_LBUTTONDOWN ,0,$0180017A); ...

  7. android多分辨率多屏幕密度下UI适配方案

    相关概念 分辨率:整个屏幕的像素数目,为了表示方便一般用屏幕的像素宽度(水平像素数目)乘以像素高度表示,形如1280x720,反之分辨率为1280x720的屏幕,像素宽度不一定为1280 屏幕密度:表 ...

  8. [原创]Android中LocationManager的简单使用,获取当前位置

    Android中LocationManager的提供了一系列方法来地理位置相关的问题,包括查询上一个已知位置:注册/注销来自某个 LocationProvider的周期性的位置更新:以及注册/注销接近 ...

  9. java---面试题---.java"源文件中可以包括多个类(不是内部类)

    答题时,先答是什么,再答有什么作用和要注意什么 一个".java"源文件中可以有多个类,但只能有一个public的类,并且public的类名必须与文件名相一致,main方法只能写在 ...

  10. Qt之进程间通信(IPC)

    简述 进程间通信,就是在不同进程之间传播或交换信息.那么不同进程之间存在着什么双方都可以访问的介质呢?进程的用户空间是互相独立的,一般而言是不能互相访问的,唯一的例外是共享内存区.但是,系统空间却是& ...