04月 08 leetcode leetcode 发表于 2021-04-08 • 字数统计 86 文章目录leetcode704. 二分查找 leetcode704. 二分查找1234567891011121314151617181920212223/** * @param {number[]} nums * @param {number} target * @return {number} */var search = function(nums, target) { const n = nums.length let l = 0, r = n - 1 while( l <= r) { const mid = Math.floor(l + (r - l) / 2) // 防止整型溢出 2^53 - 1 if (nums[mid] === target) { return mid } if (nums[mid] > target) { r = mid - 1 } else { l = mid + 1 } } return -1} 分享到: 赞赏 微信扫一扫,向我赞赏 支付宝扫一扫,向我赞赏