一.题目

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. python-selenium使用send_keys()方法写中文报错的解决方法

    问题描述: 自动化操作页面,输入中文姓名: # coding=utf-8 url = "http://dealer.bitauto.com/50002218/zuidijia/" ...

  2. 图论trainning-part-1 E. Invitation Cards

    E. Invitation Cards Time Limit: 8000ms Memory Limit: 262144KB 64-bit integer IO format: %lld      Ja ...

  3. 全库修改SQL Server现有排序规则

    近日,在项目Debug过程中发现了SQL Server排序规则冲突的问题. 由于原数据库是从英文环境的SQL中生成的,其排序规则为“SQL_Latin1_General_CP1_CI_AS”,备份到本 ...

  4. 刷题总结——魔术球问题(ssoj最小路径覆盖+网络流)

    题目: 题目描述 假设有 n 根柱子,现要按下述规则在这 n 根柱子中依次放入编号为 1,2 ,3,… 的球.(1)每次只能在某根柱子的最上面放球.(2)在同一根柱子中,任何 2 个相邻球的编号之和为 ...

  5. Uva5009 Error Curves

    已知n条二次曲线si(x) = ai*x^2 + bi*x + ci(ai ≥ 0),定义F(x) = max{si(x)},求出F(x)在[0,1000]上的最小值. 链接:传送门 分析:最大值最小 ...

  6. P2014 选课 (树形动规)

    题目描述 在大学里每个学生,为了达到一定的学分,必须从很多课程里选择一些课程来学习,在课程里有些课程必须在某些课程之前学习,如高等数学总是在其它课程之前学习.现在有N门功课,每门课有个学分,每门课有一 ...

  7. writeValueAsString封装成工具类

    封装成工具类 <span style="font-family:Microsoft YaHei;">public static String toJsonByObjec ...

  8. Linux 如何实现 VLAN

    LAN 表示 Local Area Network,本地局域网,通常使用 Hub 和 Switch 来连接 LAN 中的计算机.一般来说,两台计算机连入同一个 Hub 或者 Switch 时,它们就在 ...

  9. windows bat语法

    @echo off  表示在这条语句之后,所有执行命令的语句不会显示 setLocal    参考1 rem  注释 :或者::    参考 set /p  等待用户输入 SET /A express ...

  10. PEP8 Python编码规范(转)

    一 代码编排1 缩进.4个空格的缩进(编辑器都可以完成此功能),不使用Tap,更不能混合使用Tap和空格.2 每行最大长度79,换行可以使用反斜杠,最好使用圆括号.换行点要在操作符的后边敲回车.3 类 ...