Leetcode 16 - Sum of Three Numbers

    topic:

    Given an array of  n  integers  and a target value  . Find  the three integers in such that their sum is   closest to . Returns the sum of these three numbers. It is assumed that there is only a unique answer for each set of inputs.nums targetnums target

    This is an array problem, and it is very close to the sum of the three numbers in the 15th question, so the algorithm idea used is also very close. They are all three-pointer traversal, namely i points to the smallest bit, j is the middle bit, and k is the largest bit. There are three cases of traversal:

    1. nums[i]+nums[j]+nums[k]==target, since the answer is unique, this is the closest answer and return directly.

    2. nums[i]+nums[j]+nums[k]>target, our purpose is to find the three closest numbers to the target, so we need to make nums[i]+nums[j]+nums[k]- The target is closer to 0, and the difference between the sum of the three numbers and the target is already greater than 0, so to reduce the sum of the three numbers, you need to dial k to the left, k--;

    3.   2. nums[i]+nums[j]+nums[k]<target, for the same reason, we want the difference between the sum of the three numbers and the target to be closer to 0, so we need to increase the sum of the three numbers, so we need to Dial j to the right, j++;

    As with the 15th question, repeated questions must be considered, so an adjacent judgment needs to be added. I won't go into details here.

    Paste the code.

class Solution {
    public int threeSumClosest(int[] nums, int target) {
        int i;
        int j;
        int k;
        int result=nums[0]+nums[1]+nums[2];//initial value
        int min=Math.abs(nums[0]+nums[1]+nums[2]-target);//initial value
        Arrays.sort(nums);  
        
        for(i=0;i<nums.length-2;){
            j=i+1;
            k=nums.length-1;
            
            while(j<k){
                if(nums[i]+nums[j]+nums[k]==target){
                    return nums[i]+nums[j]+nums[k];
                }
                else if(nums[i]+nums[j]+nums[k]>target){//a+b+c-target>0, you need to turn k to the left to get closer to 0
                    
                    if(Math.abs(nums[i]+nums[j]+nums[k]-target)<min)
                    {
                        min=Math.abs(nums[i]+nums[j]+nums[k]-target);//Record the minimum value
                         result=nums[i]+nums[j]+nums[k];                                            
                    }
                    k--;
                    while(j<k&&nums[k]==nums[k+1]){
                        k--;
                    }  
                }
                
                else if(nums[i]+nums[j]+nums[k]<target){//a+b+c-target<0, you need to dial j to the right to get closer to 0
                    if(Math.abs(nums[i]+nums[j]+nums[k]-target)<min)
                    {
                        min=Math.abs(nums[i]+nums[j]+nums[k]-target);//Record the minimum value
                       result=nums[i]+nums[j]+nums[k];                                                
                    }
                   j++;
                    while(j<k&&nums[j]==nums[j-1]){
                        j++;
                    }
                }
            }
            i++;
            while(i<nums.length-2&&nums[i]==nums[i-1]){
                i++;
            }
        }
        return result;
    }
}

    The submitted results are as follows:


          Conclusion: The higher-ranked submission records basically use similar ideas, but add more exits and reduce time complexity, but generally the same.


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324808771&siteId=291194637