当前位置:网站首页>Missing digit JS in sword finger 0~n-1

Missing digit JS in sword finger 0~n-1

2022-06-09 04:57:00 Madrid Tianxin

A length of n-1 All numbers in the incremental sort array of are unique , And every number is in the range 0~n-1 within . In scope 0~n-1 Internal n There are and only one number is not in the array , Please find out the number .

Example 1:

Input : [0,1,3]
Output : 2
Example  2:

Input : [0,1,2,3,4,5,6,7,9]
Output : 8

/**
 * @param {number[]} nums
 * @return {number}
 */
var missingNumber = function(nums) {

    for(let i = 0;i<nums.length+1;i++){
        if(nums[i]!=i){
            return i
        }
        else if(nums.length===1){
            return 1
        }
    }
};

原网站

版权声明
本文为[Madrid Tianxin]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203021438576309.html