标签:while 算法详解 下界 排列 bsp 赋值 变化 last pre
function binarySearch(nums, target) { let left = 0, right = ...; while(...) { let mid = (right + left) / 2; if (nums[mid] == target) { ... } else if (nums[mid] < target) { left = ... } else if (nums[mid] > target) { right = ... } } return ...; }
因为初始化 right 的赋值是 nums.length - 1,即最后一个元素的索引,而不是 nums.length;
这也是二分查找的一个难点,本算法的「搜索区间」是两端都闭的,即 [left, right]。那么当我们发现索引 mid 对应的值不是要找的 target 时,如何确定下一步的搜索区间呢?
当然是去搜索 [left, mid - 1] 或者 [mid + 1, right] 对不对?因为 mid 已经搜索过,应该从搜索区间中去除
比如说给你有序数组 nums = [1,2,2,2,3],target = 2,此算法返回的索引是 2,没错。但是如果我想得到 target 的左侧边界,即索引 1,或者我想得到 target 的右侧边界,
即索引 3,这样的话此算法是无法处理的。
function binarySearch(nums, target) { let left = 0; let right = nums.length - 1; while (left <= right) { let mid = left + Math.floor((right - left) / 2); if (nums[mid] === target) { return mid; } else if (nums[mid] < target) { left = mid + 1; } else if (nums[mid] > target) { right = mid - 1; } } return -1; } console.log(binarySearch([1, 2, 3, 4, 5, 6, 7], 3));//2
function binarySearchLeft(nums, target) { if (nums.length == 0) return -1; let left = 0; let right = nums.length; while (left < right) { let mid = left + Math.floor((right - left) / 2); if (nums[mid] === target) { right = mid; } else if (nums[mid] < target) { left = mid + 1; } else if (nums[mid] > target) { right = mid; } } if (left == nums.length) return -1; return nums[left] === target ? left : -1; } console.log(binarySearchLeft([1, 2, 2, 2, 3], 2));//1
function binarySearchRight(nums, target) { if (nums.length == 0) return -1; let left = 0; let right = nums.length; while (left < right) { let mid = left + Math.floor((right - left) / 2); if (nums[mid] === target) { left = mid + 1; } else if (nums[mid] < target) { left = mid + 1; } else if (nums[mid] > target) { right = mid; } } if (left === 0) return -1; return nums[left - 1] === target ? left - 1 : -1; } console.log(binarySearchRight([1, 2, 2, 2, 3], 2)); //3
因为我们初始化 right = nums.length - 1 所以决定了我们的「搜索区间」是 [left, right] 所以决定了 while (left <= right) 同时也决定了 left = mid+1 和 right = mid-1 因为我们只需找到一个 target 的索引即可 所以当 nums[mid] == target 时可以立即返回
因为我们初始化 right = nums.length 所以决定了我们的「搜索区间」是 [left, right) 所以决定了 while (left < right) 同时也决定了 left = mid+1 和 right = mid 因为我们需找到 target 的最左侧索引 所以当 nums[mid] == target 时不要立即返回 而要收紧右侧边界以锁定左侧边界
因为我们初始化 right = nums.length 所以决定了我们的「搜索区间」是 [left, right) 所以决定了 while (left < right) 同时也决定了 left = mid+1 和 right = mid 因为我们需找到 target 的最右侧索引 所以当 nums[mid] == target 时不要立即返回 而要收紧左侧边界以锁定右侧边界 又因为收紧左侧边界时必须 left = mid + 1 所以最后无论返回 left 还是 right,必须减一
var searchRange1 = function (nums, target) { // let left = nums.indexOf(target); // let right = nums.lastIndexOf(target); // return [left, right]; let left = binarySearchLeft(nums, target); let right = binarySearchRight(nums, target); return [left, right]; };
var binarySearch = function (nums, target, flag) { if (nums.length == 0) return -1; let left = 0; let right = nums.length; while (left < right) { let mid = left + Math.floor((right - left) / 2); if (nums[mid] === target) { flag ? (right = mid) : (left = mid + 1); } else if (nums[mid] < target) { left = mid + 1; } else if (nums[mid] > target) { right = mid; } } if (flag && left === nums.length) return -1; if (!flag && left === 0) return -1; return flag ? nums[left] === target ? left : -1 : nums[left - 1] === target ? left - 1 : -1; }; var searchRange2 = function (nums, target) { let len = nums.length; if (len === 0) { return [-1, -1]; } let left = binarySearch(nums, target, true); if (left === -1) { return [-1, -1]; } let right = binarySearch(nums, target, false); return [left, right]; }; // console.log(searchRange2([5, 7, 7, 8, 8, 10], 8)); //[3,4]
标签:while 算法详解 下界 排列 bsp 赋值 变化 last pre
原文地址:https://www.cnblogs.com/wxh0929/p/15039864.html