16. 3Sum Closest

description:

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.

Note:

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).

my answer:

感恩

my answer

思路和上一道题3sum是一样的,就是多加了两个变量closest&diff,closest记录答案,diff记录最小差值

大佬的answer:

class Solution {
public:
    int threeSumClosest(vector<int>& nums, int target) {
        int closest = nums[0] + nums[1] + nums[2];
        int diff = abs(target - closest);
        sort(nums.begin(),nums.end());
        for (int i = 0; i < nums.size() - 2; ++i){
            int left = i + 1;
            int right = nums.size() - 1;
            while(left < right){
                int sum = nums[i] + nums[left] + nums[right];
                int newdiff = abs(target - sum);
                if(diff > newdiff){
                    diff = newdiff;
                    closest = sum;
                }
                if(sum < target) left++;
                else right--;
            }
            
        }
        return closest;
    }
};

relative point get√:

hint :

原文地址:https://www.cnblogs.com/forPrometheus-jun/p/10656867.html