Skip to content

面试题53 - II. 0~n-1中缺失的数字

示例

js
示例 1:

输入: [0,1,3]
输出: 2
示例 2:

输入: [0,1,2,3,4,5,6,7,9]
输出: 8

JS

js
/**
 * @param {number[]} nums
 * @return {number}
 */
var missingNumber = function (nums) {
    let left = 0, right = nums.length - 1
    while (left <= right) {
        mid = ~~((left + right) / 2)
        if (nums[mid] !== mid) {
            right = mid - 1
        } else {
            left = mid + 1
        }
    }
    return left
};

题链