一.题目描写叙述

二.解题技巧

该题与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. Mysql索引为什么用B+树而不用B-树

    先从数据结构的角度来看 我们知道B-树和B+树最重要的一个区别就是B+树只有叶节点存放数据,其余节点用来索引,而B-树是每个索引节点都会有Data域. 这就决定了B+树更适合用来存储外部数据,也就是所 ...

  2. Coredata 单表简单使用

    ** 使用Coredata 工程中的DataModel创建:系统创建.手动创建** ** 使用Coredata需要要导入<CoreData/CoreData.h> ** 1.系统创建(系统 ...

  3. 算法进阶面试题06——实现LFU缓存算法、计算带括号的公式、介绍和实现跳表结构

    接着第四课的内容,主要讲LFU.表达式计算和跳表 第一题 上一题实现了LRU缓存算法,LFU也是一个著名的缓存算法 自行了解之后实现LFU中的set 和 get 要求:两个方法的时间复杂度都为O(1) ...

  4. 如何在Eclipse中查看Java类库的源代码以及相应的api

    你的JDK安装目录下%Java_home%/src.zip文件就是源码,解压缩找到对应包下面的类即可. 如果是Eclipse开发,ctr+鼠标左击,出现不了源码的话,在弹出的视图中点击attach s ...

  5. FTP 其他设置

    参考文章 http://faichen.vip.blog.163.com/blog/static/37644066201010362051291/

  6. Git中的bash与CMD的区别

    Windows在使用git工具时,可以看到有两个命令输入窗: 1. Git CMD 2. Git Bash 两者的区别:Bash是基于CMD的,Bash在CMD的基础上新增了一些命令和功能,故建议使用 ...

  7. c#获取程序版本号

    Content.Text = "程序集版本:" + System.Reflection.Assembly.GetExecutingAssembly().GetName().Vers ...

  8. OSINT系列:网站信任评估WOT

     OSINT系列:网站信任评估WOT Web of Trust(WOT)是芬兰的一家网站信任评估服务公司.它通过收集用户对网站的评价,来评估网站的可信任度.在该公司网站www.mywot.com,用户 ...

  9. Javascript实现对象的创建

    能使用{}创建对象就不要使用new Object,能使用[]创建数组就不要使用new Array,JS中字面量的访问速度要高于对象. 1.通过object构造函数创建单个对象 var o = new ...

  10. 每天刷Web面试题(前10天汇总)

    一.算法题部分   1. 如何获取浏览器URL中查询字符串中的参数? function getParamsWithUrl(url) {       var args = url.split('?'); ...