Skip to content

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

示例

txt
示例 1:

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

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

JS

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

题链