mirror of
https://github.com/krahets/hello-algo.git
synced 2025-11-05 22:56:48 +08:00
Update binary search docs
This commit is contained in:
@ -29,9 +29,9 @@ const binarySearch1 = function (nums: number[], target: number): number {
|
|||||||
// 循环,当搜索区间为空时跳出(当 i = j 时为空)
|
// 循环,当搜索区间为空时跳出(当 i = j 时为空)
|
||||||
while (i < j) {
|
while (i < j) {
|
||||||
const m = Math.floor(i + (j - i) / 2); // 计算中点索引 m
|
const m = Math.floor(i + (j - i) / 2); // 计算中点索引 m
|
||||||
if (nums[m] < target) { // 此情况说明 target 在区间 [m+1, j) 中
|
if (nums[m] < target) { // 此情况说明 target 在区间 [m+1, j] 中
|
||||||
i = m + 1;
|
i = m + 1;
|
||||||
} else if (nums[m] > target) { // 此情况说明 target 在区间 [i, m) 中
|
} else if (nums[m] > target) { // 此情况说明 target 在区间 [i, m] 中
|
||||||
j = m;
|
j = m;
|
||||||
} else { // 找到目标元素,返回其索引
|
} else { // 找到目标元素,返回其索引
|
||||||
return m;
|
return m;
|
||||||
|
|||||||
Reference in New Issue
Block a user