Update binary tree (JS).

This commit is contained in:
Yudong Jin
2022-12-23 01:25:12 +08:00
parent ee6842dc9c
commit 22314be33c
4 changed files with 15 additions and 24 deletions

View File

@@ -29,31 +29,24 @@ $$
首先,我们先采用“双闭区间”的表示,在数组 `nums` 中查找目标元素 `target` 的对应索引。
=== "Step 1"
![binary_search_step1](binary_search.assets/binary_search_step1.png)
=== "Step 2"
![binary_search_step2](binary_search.assets/binary_search_step2.png)
=== "Step 3"
![binary_search_step3](binary_search.assets/binary_search_step3.png)
=== "Step 4"
![binary_search_step4](binary_search.assets/binary_search_step4.png)
=== "Step 5"
![binary_search_step5](binary_search.assets/binary_search_step5.png)
=== "Step 6"
![binary_search_step6](binary_search.assets/binary_search_step6.png)
=== "Step 7"
![binary_search_step7](binary_search.assets/binary_search_step7.png)
二分查找“双闭区间”表示下的代码如下所示。
@@ -152,13 +145,13 @@ $$
let i = 0, j = nums.length - 1;
// 循环,当搜索区间为空时跳出(当 i > j 时为空)
while (i <= j) {
let m = parseInt((i + j) / 2); // 计算中点索引 m ,在 JS 中需使用 parseInt 函数取整
if (nums[m] < target) // 此情况说明 target 在区间 [m+1, j] 中
let m = parseInt((i + j) / 2); // 计算中点索引 m ,在 JS 中需使用 parseInt 函数取整
if (nums[m] < target) // 此情况说明 target 在区间 [m+1, j] 中
i = m + 1;
else if (nums[m] > target) // 此情况说明 target 在区间 [i, m-1] 中
else if (nums[m] > target) // 此情况说明 target 在区间 [i, m-1] 中
j = m - 1;
else
return m; // 找到目标元素,返回其索引
return m; // 找到目标元素,返回其索引
}
// 未找到目标元素,返回 -1
return -1;