Likou brushing notes day7 (repeated numbers in the array + search for numbers in the sorted array + missing numbers from 0 to n-1)

repeated numbers in array

topic

insert image description here

train of thought

Create a hash table, traverse the array, add it to the hash table if it does not exist, and return it directly if it exists

the code

var findRepeatNumber = function(nums) {
    
    
    let map = new Map();
    for(let i=0;i<nums.length;i++)
    {
    
    
        if(!map.has(nums[i]))
        {
    
    
            map.set(nums[i],i)
        }else{
    
    
            return nums[i]
        }
    }
    return null
};

Find data in sorted array

topic

insert image description here

train of thought

Define targetLeft and targetRight as the following table equal to the two sides of the target value, then use binary search to get targetLeft and targetRight, and finally subtract the two and add 1 to get the result; the steps to calculate targetLeft are as follows
:

  • First define targetLeft as a random value, such as -1; define the left and right pointers, pointing to the first element and the last element of the array respectively

insert image description here

  • Then calculate the middle pointer mid, as shown in the figure 2, judge nums[2]=7<target

insert image description here

  • Then make left=mid+1

insert image description here

  • Then calculate mid=4, and nums[4]==target

insert image description here

  • targetLeft=mid

insert image description here

  • right=mid-1

insert image description here

  • At this time left=right, so continue to loop, calculate mid=3, and nums[3]=target

insert image description here

  • So let targetLeft=mid=3

insert image description here

  • Let right=mid-1, at this time right<left, jump out of the loop.

insert image description here
The steps to find targetRight are the same.

the code

var search = function (nums, target) {
    
    
    var targetLeft = -1,
        targetRight = -1,
        left=0,
        right=nums.length-1;
    //找出该数在左边第一次出现的位置
    while (left<=right) {
    
    
        var mid = Math.floor((left + right) / 2);
        if (target == nums[mid]) {
    
    
            targetLeft = mid;   //记录下第一次出现的下标
            right=mid - 1;    //继续向前查找是否该数出现过
        } else if (target < nums[mid]) {
    
    
            right = mid -1;
        } else {
    
    
            left = mid + 1;
        }
    }
    //重置 left 和 right 的值,找出该数在右边最后一个出现的位置
    left = 0;
    right = nums.length - 1;
    while (left <= right) {
    
    
        var mid = Math.floor((left + right) / 2);
        if (target == nums[mid]) {
    
    
            targetRight = mid;
            left = mid + 1;
        } else if (target < nums[mid]) {
    
    
            right = mid - 1;
        } else {
    
    
            left = mid + 1;
        }
    }
    return targetLeft<=targetRight&&targetLeft!==-1 ? targetRight - targetLeft + 1 : 0;
};

Missing digits from 0 to n-1

topic

insert image description here

train of thought

Use mathematical formulas to calculate the sum of 0-n, and then subtract the sum of nums to get the missing number.

the code

var missingNumber = function(nums) {
    
    
    const n = nums.length + 1;
    let total = Math.floor(n * (n - 1) / 2);
    let arrSum = 0;
    for (let i = 0; i < n - 1; i++) {
    
    
        arrSum += nums[i];
    }
    return total - arrSum;
};

Guess you like

Origin blog.csdn.net/weixin_51610980/article/details/128408614