mirror of
https://github.com/krahets/hello-algo.git
synced 2026-07-10 06:26:08 +00:00
feat: Revised the book (#978)
* Sync recent changes to the revised Word. * Revised the preface chapter * Revised the introduction chapter * Revised the computation complexity chapter * Revised the chapter data structure * Revised the chapter array and linked list * Revised the chapter stack and queue * Revised the chapter hashing * Revised the chapter tree * Revised the chapter heap * Revised the chapter graph * Revised the chapter searching * Reivised the sorting chapter * Revised the divide and conquer chapter * Revised the chapter backtacking * Revised the DP chapter * Revised the greedy chapter * Revised the appendix chapter * Revised the preface chapter doubly * Revised the figures
This commit is contained in:
@@ -4,7 +4,7 @@
|
||||
|
||||
!!! question
|
||||
|
||||
给定一个长度为 $n$ 的有序数组 `nums` ,数组可能包含重复元素。请返回数组中最左一个元素 `target` 的索引。若数组中不包含该元素,则返回 $-1$ 。
|
||||
给定一个长度为 $n$ 的有序数组 `nums` ,其中可能包含重复元素。请返回数组中最左一个元素 `target` 的索引。若数组中不包含该元素,则返回 $-1$ 。
|
||||
|
||||
回忆二分查找插入点的方法,搜索完成后 $i$ 指向最左一个 `target` ,**因此查找插入点本质上是在查找最左一个 `target` 的索引**。
|
||||
|
||||
@@ -13,7 +13,7 @@
|
||||
- 插入点的索引 $i$ 越界。
|
||||
- 元素 `nums[i]` 与 `target` 不相等。
|
||||
|
||||
当遇到以上两种情况时,直接返回 $-1$ 即可。
|
||||
当遇到以上两种情况时,直接返回 $-1$ 即可。代码如下所示:
|
||||
|
||||
```src
|
||||
[file]{binary_search_edge}-[class]{}-[func]{binary_search_left_edge}
|
||||
@@ -21,7 +21,7 @@
|
||||
|
||||
## 查找右边界
|
||||
|
||||
那么如何查找最右一个 `target` 呢?最直接的方式是修改代码,替换在 `nums[m] == target` 情况下的指针收缩操作。代码在此省略,有兴趣的同学可以自行实现。
|
||||
那么如何查找最右一个 `target` 呢?最直接的方式是修改代码,替换在 `nums[m] == target` 情况下的指针收缩操作。代码在此省略,有兴趣的读者可以自行实现。
|
||||
|
||||
下面我们介绍两种更加取巧的方法。
|
||||
|
||||
@@ -33,7 +33,7 @@
|
||||
|
||||

|
||||
|
||||
请注意,返回的插入点是 $i$ ,因此需要将其减 $1$ ,从而获得 $j$ 。
|
||||
请注意,返回的插入点是 $i$ ,因此需要将其减 $1$ ,从而获得 $j$ :
|
||||
|
||||
```src
|
||||
[file]{binary_search_edge}-[class]{}-[func]{binary_search_right_edge}
|
||||
@@ -50,7 +50,7 @@
|
||||
|
||||

|
||||
|
||||
代码在此省略,值得注意以下两点。
|
||||
代码在此省略,以下两点值得注意。
|
||||
|
||||
- 给定数组不包含小数,这意味着我们无须关心如何处理相等的情况。
|
||||
- 因为该方法引入了小数,所以需要将函数中的变量 `target` 改为浮点数类型。
|
||||
|
||||
Reference in New Issue
Block a user