一.题目描写叙述

二.解题技巧

该题与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. UVA 10976 分数拆分【暴力】

    题目链接:https://vjudge.net/contest/210334#problem/C 题目大意: It is easy to see that for every fraction in ...

  2. 关于 tensorflow-gpu 中 CUDA 和 CuDNN 版本适配问题

    问题 今天在使用 tensorflow-yolov3 的时候,发现报错 Loaded runtime CuDNN library: but source was compiled with: . Cu ...

  3. python单元测试模块unittest

    1.unittest_demo.py # coding=utf-8 import time import unittest from HtmlTestRunner import HTMLTestRun ...

  4. 多臂机测试, AB测试

    bandit  强盗,土匪:恶棍:敲诈者 ['bændɪt] 多臂机 multi-armed bandit MAB  简写. one-arm bandit   tiger ji 是一种自动AB测试的方 ...

  5. 洛谷.2051.[AHOI2009]中国象棋(DP)

    题目链接 /* 每行每列不能超过2个棋子,求方案数 前面行对后面行的影响只有 放了0个.1个.2个 棋子的列数,与排列方式无关 所以设f[i][j][k]表示前i行,放了0个棋子的有j列,放了1个棋子 ...

  6. node模块包装为Promise书写法

    1. const Promise = require('bluebird') const fs = Promise.promisifyAll(Promise.promisify(require('fs ...

  7. C++学习笔记42:进程管理

    子进程异步清除 SIGCHLD信号:子进程终止时,向父进程自动发送,编写此信号处理例程,异步清除子进程 #include <signal.h> #include <string.h& ...

  8. nginx 方向代理 jenkins

    环境 10.0.0.20 Nginx 10.0.0.21 jenkins 10.0.0.20 nginx 进入到nginx目录,去除无用字段输入到conf.d/jenkins.conf 文件中 [ro ...

  9. memcache 在php中的用法

    memcached 的安装方法详见我博客的另一个页面:http://www.cnblogs.com/chrdai/p/6656443.html 用法: 一.memcache 连接命令: 1.memca ...

  10. Instruments

    链接: iOS性能优化:Instruments使用实战 iOS 使用Instruments的工具小结