【题目描述】

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.

Notice:You may assume that each input would have exactly one solution.

给一个包含 n 个整数的数组 S, 找到和与给定整数 target 最接近的三元组,返回这三个数的和。

注意:只需要返回三元组之和,无需返回三元组本身

【题目链接】

www.lintcode.com/en/problem/3sum-closest/

【题目解析】

思路,类似于 3 Sum 问题,不同之处在于寻找与target的绝对值最小的数;同样可以利用two pointers,对于 a + b + c 中的 b, c 来作为两个指针,a 为 num[i], 那么b初始值则为 num[i + 1], c初始值为num[len - 1]; 通过比较每次 a + b + c 的sum与target的大小,来确定移动b,或者移动c。

【参考答案】

www.jiuzhang.com/solutions/3sum-closest/