This commit is contained in:
krahets
2023-07-26 08:58:52 +08:00
parent 583d89729e
commit 6381f16506
45 changed files with 198 additions and 198 deletions
+3 -3
View File
@@ -16,9 +16,9 @@ comments: true
1. 计算中点索引 $m = \lfloor {(i + j) / 2} \rfloor$ ,其中 $\lfloor \space \rfloor$ 表示向下取整操作。
2. 判断 `nums[m]``target` 的大小关系,分为三种情况:
1.`nums[m] < target` 时,说明 `target` 在区间 $[m + 1, j]$ 中,因此执行 $i = m + 1$
2.`nums[m] > target` 时,说明 `target` 在区间 $[i, m - 1]$ 中,因此执行 $j = m - 1$
3.`nums[m] = target` 时,说明找到 `target` ,因此返回索引 $m$
1.`nums[m] < target` 时,说明 `target` 在区间 $[m + 1, j]$ 中,因此执行 $i = m + 1$
2.`nums[m] > target` 时,说明 `target` 在区间 $[i, m - 1]$ 中,因此执行 $j = m - 1$
3.`nums[m] = target` 时,说明找到 `target` ,因此返回索引 $m$
若数组不包含目标元素,搜索区间最终会缩小为空。此时返回 $-1$ 。