一.题目

3Sum Closest

Total Accepted: 32191 Total
Submissions: 119262My
Submissions

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).
Show Tags
Have you met this question in a real interview?

Yes
No

Discuss












二.解题技巧

    这道题和另外一道题3Sum类似,主要流程也是一样的。也是先进行排序,然后依照顺序选择数组A中的第i个元素作为三个数的最小值来寻找三个数的和。只是的地方在于这里是寻找最靠近给定值,寻找最靠近的值就无全部反复的事情了。所以能够不考虑夹逼的过程中的越过同样元素的过程,尽管越过同样的元素速度会快一些,可是代码长度也会加长。
    这道题难的地方可能在于刚開始这样的差的阈值的过程。假设把阈值设置得太小了,会出现错误,因此,应该尽可能地将阈值设置得大一点。因为数组是已经排序的,因此,数组中三个数的和的范围在[3*A[0], 3*A[n-1]],因此,阈值能够依据以下三种情况进行设置:
1.if target >= 3*A[n-1],阈值设置为H = target - 3 * A[0];
2.if 3*A[0] <= target<3*A[n-1],阈值设置为H = 3 * A[n-1] - 3*A[0];
3.if target < 3 * A[0],阈值设置为H = 3 * A[n-1] - target。

这样就能够依据阈值与眼下得到的三个数的和与target的差来推断是否是最接近target的情况了,依据不同的情况,选择缩放的方向。



三.实现代码

class Solution
{
public:
int threeSumClosest(vector<int> &num, int target)
{
int Size = num.size(); sort(num.begin(), num.end()); int MaxSum = 3 * num[Size - 1];
int MinSum = 3 * num[0];
int ThreadHold = 0;
if (target <= MinSum)
{
ThreadHold = MaxSum - target;
}
if (MaxSum < target)
{
ThreadHold = target - MinSum;
}
if ((MinSum < target) && (target <= MaxSum))
{
ThreadHold = MaxSum - MinSum;
} int Result = 0; for (int Index_outter = 0; Index_outter < (Size - 2); Index_outter++)
{
int First = num[Index_outter];
int Second = num[Index_outter + 1]; if ((Index_outter != 0) && (First == num[Index_outter - 1]))
{
continue;
} int Start = Index_outter + 1;
int End = Size - 1; while (Start < End)
{
Second = num[Start];
int Third = num[End]; int Sum = First + Second + Third; if (Sum == target)
{
return Sum;
} if (Sum < target)
{
Start++;
if (ThreadHold >= (target - Sum))
{
Result = Sum;
ThreadHold = target - Sum;
}
} if (Sum > target)
{
End--;
if (ThreadHold >= (Sum - target))
{
Result = Sum;
ThreadHold = Sum - target;
}
}
}
}
return Result; }
};



四.体会

    这道题最难的地方在于阈值的选择上面。事实上能够设置为整数的最大值的,可是,我一開始并不知道怎样计算整数的最大值,因此。仅仅能依据排好序的数组的三个数的和的范围与target的关系来设定阈值了,详细的阈值设置情况能够画个数轴出来分析,画出数轴之后,一切就明显了。




版权全部。欢迎转载,转载请注明出处,谢谢









LeetCode_3Sum Closest的更多相关文章

  1. [LeetCode] Closest Binary Search Tree Value II 最近的二分搜索树的值之二

    Given a non-empty binary search tree and a target value, find k values in the BST that are closest t ...

  2. [LeetCode] Closest Binary Search Tree Value 最近的二分搜索树的值

    Given a non-empty binary search tree and a target value, find the value in the BST that is closest t ...

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

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

  4. ICP算法(Iterative Closest Point迭代最近点算法)

    标签: 图像匹配ICP算法机器视觉 2015-12-01 21:09 2217人阅读 评论(0) 收藏 举报 分类: Computer Vision(27) 版权声明:本文为博主原创文章,未经博主允许 ...

  5. 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 ...

  6. 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 ...

  7. K closest points

    Find the K closest points to a target point in a 2D plane. class Point { public int x; public int y; ...

  8. 16. 3Sum Closest

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

  9. jquery:closest和parents的主要区别

    closest和parents的主要区别是:1,前者从当前元素开始匹配寻找,后者从父元素开始匹配寻找:2,前者逐级向上查找,直到发现匹配的元素后就停止了,后者一直向上查找直到根元素,然后把这些元素放进 ...

随机推荐

  1. 【0门槛】PR稿的自我修养

    本文来自网易云社区 作者:巩爽 十一过完,离2018年结束就只剩下85天啦!是不是2016年许下的2017年的梦想,在2018年还没有实现? 做过的项目仿佛都小有成就,可惜只是内部自嗨,想做域外宣传却 ...

  2. jquery插件编写【转载】

    如今做web开发,jquery 几乎是必不可少的,就连vs神器在2010版本开始将Jquery 及ui 内置web项目里了.至于使用jquery好处这里就不再赘述了,用过的都知道.今天我们来讨论下jq ...

  3. 【LeetCode】Jewels and Stones(宝石与石头)

    这道题是LeetCode里的第771道题. 题目要求: 给定字符串J 代表石头中宝石的类型,和字符串 S代表你拥有的石头. S 中每个字符代表了一种你拥有的石头的类型,你想知道你拥有的石头中有多少是宝 ...

  4. 九度oj 题目1091:棋盘游戏

    题目描述: 有一个6*6的棋盘,每个棋盘上都有一个数值,现在又一个起始位置和终止位置,请找出一个从起始位置到终止位置代价最小的路径:    1.只能沿上下左右四个方向移动    2.总代价是没走一步的 ...

  5. Bzoj1083 1083: [SCOI2005]繁忙的都市【MST】

    大水题,真不知道出题者是怎么把这么水的题出的这么长的TAT 其实这题在于考语文水平,一共三个要求,前两个要求意思就是要选出的道路是树形的,最后一个要求就是要权值最小,于是整个题意说白了就是求一棵MST ...

  6. 【2018.4.5】Shoi2017题集

    这三道题分别对应bzoj4868~4870,pdf没法往这放,因此放弃了. T1: 方法1(正解):三分法 考虑暴力枚举最晚公布的时间x,关注到2操作是没有负面影响的1操作,所以如果A大于B,那么只需 ...

  7. LinkedList的构造函数有哪些

    LinkedList构造函数有(两种): public LinkedList() public LinkedList(Collection<? extends E> c) /** * Co ...

  8. sqlserver通过设计器修改表结构保存时提示:保存到文本问题

    在sqlserver通过设计器修改表结构后保存时提示:保存到文本问题,这个问题可能通过修改设置项解决 工具>选项>设计器>   在弹出的窗口是把“阻止保存要求重新创建表的更改”选项的 ...

  9. 洛谷 [P2964] 硬币的游戏

    博弈论+dp 依旧是博弈论的壳子,但问的是最大值,所以要dp 设 dp[i][j] 表示该取 i 号硬币,上一次取了 j 个的先手能取的最大值, 因为每次从小到大枚举复杂度太高,所以我们要从 dp[i ...

  10. 【Codeforces Round #505 (Div. 1 + Div. 2) 】

    A:https://www.cnblogs.com/myx12345/p/9843966.html B: C:https://www.cnblogs.com/myx12345/p/9844084.ht ...