一.题目描写叙述

二.解题技巧

该题与3Sum的要求类似。不同的是要求选出的组合的和与目标值target最接近而不一定相等。但实际上,与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 16. 3Sum Closest(最接近的三数之和)

    LeetCode 16. 3Sum Closest(最接近的三数之和)

  2. 【leetcode】3Sum Closest

    3Sum Closest Given an array S of n integers, find three integers in S such that the sum is closest t ...

  3. [Leetcode][016] 3Sum Closest (Java)

    题目: https://leetcode.com/problems/3sum-closest/ [标签]Array; Two Pointers [个人分析] 这道题和它的姊妹题 3Sum 非常类似, ...

  4. Leetcode 16. 3Sum Closest(指针搜索)

    16. 3Sum Closest Medium 131696FavoriteShare Given an array nums of n integers and an integer target, ...

  5. LeetCode (13): 3Sum Closest

    https://leetcode.com/problems/3sum-closest/ [描述] Given an array S of n integers, find three integers ...

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

    Given an array nums of n integers and an integer target, find three integers in nums such that the s ...

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

  8. Java [leetcode 16] 3Sum Closest

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

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

  10. [LeetCode][Java] 3Sum Closest

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

随机推荐

  1. java语言打印上三角和下三角,进一步得到九九乘法表

    关于下面两种图形的打印问题 ***** 与 * ****  ** *** *** ** **** *  ***** 一:程序 1.先打印下三角 2.结果 3.后打印上三角 4.结果 二:知识点 1.f ...

  2. 全排列问题(递归&非递归&STL函数)

    问题描述: 打印输出1-9的所有全排序列,或者打印输出a-d的全排列. 思路分析: 将每个元素放到余下n-1个元素组成的队列最前方,然后对剩余元素进行全排列,依次递归下去. 比如:1 2 3 为例首先 ...

  3. python中confIgparser模块学习

    python中configparser模块学习 ConfigParser模块在python中用来读取配置文件,配置文件的格式跟windows下的ini配置文件相似,可以包含一个或多个节(section ...

  4. 【值得收藏】一份非常完整的Mysql规范

    做一个积极的人 编码.改bug.提升自己 我有一个乐园,面向编程,春暖花开! 本文从芋道源码转载,在原有内容基础上结合阿里巴巴Java开发手册中Mysql数据库章节的介绍,加上自己的理解和说明,整理而 ...

  5. 使用perf工具导致系统hang死的原因

    [perf工具导致系统hang住的原因是触发了低版本kernel的bug] 今天在测试服务器做压测,运行perf record做性能分析时,系统再次hang住了,这次在系统日志中记录了一些有用的信息, ...

  6. osds have slow requests

    ceph health detailHEALTH_WARN 14 requests are blocked > 32 sec; 11 osds have slow requests7 ops a ...

  7. ajax批量删除功能的实现源代码

    效果展示: 完整代码如下: <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charse ...

  8. POJ 3243 Clever Y 扩展BSGS

    http://poj.org/problem?id=3243 这道题的输入数据输入后需要将a和b都%p https://blog.csdn.net/zzkksunboy/article/details ...

  9. CSDN博客文章的备份及导出电子书CHM

    需要用到的工具集合下载:http://download.csdn.net/source/2881423 在CSDN.百度等写博客文章的应该很多,很多时候担心服务器有一天突然挂了,或者担心自己的号被封了 ...

  10. (Tcp协议)linux上netstat -atunlp后出现的数据的意思(socket状态)

    https://zhidao.baidu.com/question/486077599.html 1.netstat命令的-t参数指的是 tcp的简写,意思是仅显示tcp相关选项2.示例:列出所有 t ...