1. 题意
求最接近的三数之和
最接近的三数之和
2. 题解
与三数之和类似
class Solution {
public:int threeSumClosest(vector<int>& nums, int target) {sort(nums.begin(), nums.end());int sz = nums.size();int sum = nums[0] + nums[1] + nums[2];for (int i = 0;i < sz; ++i) {int lp = i + 1;int rp = sz - 1;if ( i && nums[i - 1] == nums[i])continue;int tot ;while (lp < rp) {tot = nums[i];tot += nums[lp] + nums[rp];if ( abs( tot - target) < abs(sum - target))sum = tot;if ( tot < target) {++lp;}else {--rp;}}}return sum;}
};