mirror of
https://github.com/krahets/hello-algo.git
synced 2026-07-24 12:06:07 +00:00
Add the section of binary search insertion. (#671)
Refactor the section of binary search edge. Finetune the figures of binary search.
This commit is contained in:
@@ -6,7 +6,9 @@
|
||||
|
||||
给定一个长度为 $n$ 的数组 `nums` ,元素按从小到大的顺序排列,数组不包含重复元素。请查找并返回元素 `target` 在该数组中的索引。若数组不包含该元素,则返回 $-1$ 。
|
||||
|
||||
对于上述问题,我们先初始化指针 $i = 0$ 和 $j = n - 1$ ,分别指向数组首元素和尾元素,代表搜索区间 $[0, n - 1]$ 。其中,中括号表示“闭区间”,即包含边界值本身。
|
||||

|
||||
|
||||
对于上述问题,我们先初始化指针 $i = 0$ 和 $j = n - 1$ ,分别指向数组首元素和尾元素,代表搜索区间 $[0, n - 1]$ 。请注意,中括号表示闭区间,其包含边界值本身。
|
||||
|
||||
接下来,循环执行以下两个步骤:
|
||||
|
||||
@@ -18,9 +20,6 @@
|
||||
|
||||
若数组不包含目标元素,搜索区间最终会缩小为空。此时返回 $-1$ 。
|
||||
|
||||
=== "<0>"
|
||||

|
||||
|
||||
=== "<1>"
|
||||

|
||||
|
||||
|
||||
Reference in New Issue
Block a user