其他分享
首页 > 其他分享> > 16. 3Sum Closest[M]最接近的三数之和

16. 3Sum Closest[M]最接近的三数之和

作者:互联网

题目

Given an array nums of n integers and an integer target, find three integers in nums such that the sum is closest to target. Return the sum of the three integers. You may assume that each input would have exactly one solution.
Example:
  Given array nums = [-1, 2, -1, -4], and target = 1.
  The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).

思路

对于一个数组S,求数组中三个数a、b、c的和,使之最接近target。将问题抽象化为,求一个min使得:
\[min = arg \ min | \ target - min \ | \quad s.t. \quad min =a+b+c\]
即求出使得$|  target - min  | $时min的值。
三指针法,见题[15]。三指针方法的关键是:

标签:target,Closest,nums,3Sum,三数,sum,min,int,指针
来源: https://www.cnblogs.com/Jessey-Ge/p/10993493.html