16. 3Sum Closest
基本解法
class Solution { public: int threeSumClosest(vector<int> &nums, int target) { int N = nums.size(); int s = 10000; sort(nums.begin(), nums.end()); for (int first = 0; first < N - 2; first++) { int second = first + 1; int third = N - 1; while (second < third) { int tmps = nums[first] + nums[second] + nums[third]; s = abs(target - tmps) < abs(target - s) ? tmps : s; if (tmps > target) { third--; } else if (tmps < target) { second++; } else { return target; } } } return s; } };
和15题解法类似。