The closest sum of three numbers of Leetcode-16

Title Link
Similar topics: The sum of three numbers of Leetcode-15

Sort + double pointer

Sort the array nums, lock a number nums[i], and then use the double pointer to consider the sum of the remaining two numbers
Dual pointer initialization: LF = i + 1 (no need to start from scratch, if there is this combination, it will be found when i is smaller)
For each value of ans, the absolute difference between res and ans and target is smaller

class Solution {
public:
    int threeSumClosest(vector<int>& nums, int target) {
        sort(nums.begin(),nums.end());
        int res=INT_MAX/2;
        int lf,rt;
        for(int i=0;i<nums.size();i++)
        {            
            lf=i+1;
            rt=nums.size()-1;
            while(lf<rt)
            {
                int ans=nums[i]+nums[lf]+nums[rt];
                if(ans==target)
                {
                    return target;
                }   
                else if(ans<target)
                {
                    res=fabs(res-target)>fabs(ans-target)?ans:res;
                    lf++;
                }
                else
                {
                    res=fabs(res-target)>fabs(ans-target)?ans:res;
                    rt--;
                }     
            }
        }
        return res;
    }
};

Consider weight removal

For the de duplication of nums[i]
When i is small, the remaining two numbers are considered more widely.
For example, for the [1,1,1,2,3], target=7 group, for the first 1, the most appropriate [2,3] will be found from [1,1,2,3]. The second 1 and the third 1 are only the search scope is decreasing, so we can consider de duplication.

De duplication of nums[lf] and nums[rt]
Similarly, suppose the input changes to [1,1,1,3,3,3], target=6
For the first 1, the first group found in [1,1,3,3,3] is nums[lf]=1, nums[rt]=3
After that, the search range is tightened inward, but if the nums[lf] or nums[rt] value is the same as the last time, then it is unnecessary to add and sum again for ans, and only the changed value can bring more close results

class Solution {
public:
    int threeSumClosest(vector<int>& nums, int target) {
        sort(nums.begin(),nums.end());
        int res=INT_MAX/2;
        int lf,rt;
        for(int i=0;i<nums.size();i++)
        {         
            if(i>0&&nums[i-1]==nums[i])
                continue;
            lf=i+1;
            rt=nums.size()-1;
            while(lf<rt)
            {
                int ans=nums[i]+nums[lf]+nums[rt];                
                if(ans==target)
                    return target;
                res=fabs(res-target)>fabs(ans-target)?ans:res;
                if(ans<target)
                {    
                    lf++;
                    while(lf<rt&&nums[lf]==nums[lf-1])
                        lf++;
                }
                else
                {
                    rt--;
                    while(lf<rt&&nums[rt]==nums[rt+1])
                        rt--;
                }     
            }
        }
        return res;
    }
};

Posted on Thu, 04 Jun 2020 13:27:00 -0400 by Lashiec