2023-05-21 19:04:21 +08:00
|
|
|
|
# 二分查找边界
|
|
|
|
|
|
2023-08-04 05:16:56 +08:00
|
|
|
|
## 查找左边界
|
2023-05-21 19:04:21 +08:00
|
|
|
|
|
2023-05-22 01:37:12 +08:00
|
|
|
|
!!! question
|
2023-05-21 19:04:21 +08:00
|
|
|
|
|
2023-08-04 05:16:56 +08:00
|
|
|
|
给定一个长度为 $n$ 的有序数组 `nums` ,数组可能包含重复元素。请返回数组中最左一个元素 `target` 的索引。若数组中不包含该元素,则返回 $-1$ 。
|
2023-05-21 19:04:21 +08:00
|
|
|
|
|
2023-08-06 23:19:37 +08:00
|
|
|
|
回忆二分查找插入点的方法,搜索完成后 $i$ 指向最左一个 `target` ,**因此查找插入点本质上是在查找最左一个 `target` 的索引**。
|
2023-05-21 19:04:21 +08:00
|
|
|
|
|
2023-08-27 22:49:47 +08:00
|
|
|
|
考虑通过查找插入点的函数实现查找左边界。请注意,数组中可能不包含 `target` ,这种情况可能导致以下两种结果。
|
2023-05-22 01:37:12 +08:00
|
|
|
|
|
2023-08-27 22:49:47 +08:00
|
|
|
|
- 插入点的索引 $i$ 越界。
|
|
|
|
|
- 元素 `nums[i]` 与 `target` 不相等。
|
2023-05-22 01:37:12 +08:00
|
|
|
|
|
2023-08-04 05:16:56 +08:00
|
|
|
|
当遇到以上两种情况时,直接返回 $-1$ 即可。
|
2023-05-21 19:04:21 +08:00
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
=== "Python"
|
2023-05-21 19:04:21 +08:00
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
```python title="binary_search_edge.py"
|
|
|
|
|
[class]{}-[func]{binary_search_left_edge}
|
2023-05-21 19:04:21 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C++"
|
|
|
|
|
|
|
|
|
|
```cpp title="binary_search_edge.cpp"
|
|
|
|
|
[class]{}-[func]{binarySearchLeftEdge}
|
|
|
|
|
```
|
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
=== "Java"
|
2023-05-21 19:04:21 +08:00
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
```java title="binary_search_edge.java"
|
|
|
|
|
[class]{binary_search_edge}-[func]{binarySearchLeftEdge}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C#"
|
|
|
|
|
|
|
|
|
|
```csharp title="binary_search_edge.cs"
|
2023-10-08 01:33:46 +08:00
|
|
|
|
[class]{binary_search_edge}-[func]{BinarySearchLeftEdge}
|
2023-05-21 19:04:21 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Go"
|
|
|
|
|
|
|
|
|
|
```go title="binary_search_edge.go"
|
|
|
|
|
[class]{}-[func]{binarySearchLeftEdge}
|
|
|
|
|
```
|
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
=== "Swift"
|
|
|
|
|
|
|
|
|
|
```swift title="binary_search_edge.swift"
|
|
|
|
|
[class]{}-[func]{binarySearchLeftEdge}
|
|
|
|
|
```
|
|
|
|
|
|
2023-07-26 15:35:38 +08:00
|
|
|
|
=== "JS"
|
2023-05-21 19:04:21 +08:00
|
|
|
|
|
|
|
|
|
```javascript title="binary_search_edge.js"
|
|
|
|
|
[class]{}-[func]{binarySearchLeftEdge}
|
|
|
|
|
```
|
|
|
|
|
|
2023-07-26 15:35:38 +08:00
|
|
|
|
=== "TS"
|
2023-05-21 19:04:21 +08:00
|
|
|
|
|
|
|
|
|
```typescript title="binary_search_edge.ts"
|
|
|
|
|
[class]{}-[func]{binarySearchLeftEdge}
|
|
|
|
|
```
|
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
=== "Dart"
|
2023-05-21 19:04:21 +08:00
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
```dart title="binary_search_edge.dart"
|
2023-05-21 19:04:21 +08:00
|
|
|
|
[class]{}-[func]{binarySearchLeftEdge}
|
|
|
|
|
```
|
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
=== "Rust"
|
2023-05-21 19:04:21 +08:00
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
```rust title="binary_search_edge.rs"
|
|
|
|
|
[class]{}-[func]{binary_search_left_edge}
|
2023-05-21 19:04:21 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
=== "C"
|
2023-05-21 19:04:21 +08:00
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
```c title="binary_search_edge.c"
|
2023-05-21 19:04:21 +08:00
|
|
|
|
[class]{}-[func]{binarySearchLeftEdge}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Zig"
|
|
|
|
|
|
|
|
|
|
```zig title="binary_search_edge.zig"
|
|
|
|
|
[class]{}-[func]{binarySearchLeftEdge}
|
|
|
|
|
```
|
|
|
|
|
|
2023-05-22 01:37:12 +08:00
|
|
|
|
## 查找右边界
|
2023-05-21 19:04:21 +08:00
|
|
|
|
|
2023-08-04 05:16:56 +08:00
|
|
|
|
那么如何查找最右一个 `target` 呢?最直接的方式是修改代码,替换在 `nums[m] == target` 情况下的指针收缩操作。代码在此省略,有兴趣的同学可以自行实现。
|
|
|
|
|
|
|
|
|
|
下面我们介绍两种更加取巧的方法。
|
|
|
|
|
|
|
|
|
|
### 复用查找左边界
|
|
|
|
|
|
|
|
|
|
实际上,我们可以利用查找最左元素的函数来查找最右元素,具体方法为:**将查找最右一个 `target` 转化为查找最左一个 `target + 1`**。
|
2023-05-21 19:04:21 +08:00
|
|
|
|
|
2023-08-21 19:33:45 +08:00
|
|
|
|
如下图所示,查找完成后,指针 $i$ 指向最左一个 `target + 1`(如果存在),而 $j$ 指向最右一个 `target` ,**因此返回 $j$ 即可**。
|
2023-08-04 05:16:56 +08:00
|
|
|
|
|
|
|
|
|
![将查找右边界转化为查找左边界](binary_search_edge.assets/binary_search_right_edge_by_left_edge.png)
|
|
|
|
|
|
|
|
|
|
请注意,返回的插入点是 $i$ ,因此需要将其减 $1$ ,从而获得 $j$ 。
|
2023-05-21 19:04:21 +08:00
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
=== "Python"
|
2023-05-21 19:04:21 +08:00
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
```python title="binary_search_edge.py"
|
|
|
|
|
[class]{}-[func]{binary_search_right_edge}
|
2023-05-21 19:04:21 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C++"
|
|
|
|
|
|
|
|
|
|
```cpp title="binary_search_edge.cpp"
|
|
|
|
|
[class]{}-[func]{binarySearchRightEdge}
|
|
|
|
|
```
|
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
=== "Java"
|
2023-05-21 19:04:21 +08:00
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
```java title="binary_search_edge.java"
|
|
|
|
|
[class]{binary_search_edge}-[func]{binarySearchRightEdge}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C#"
|
|
|
|
|
|
|
|
|
|
```csharp title="binary_search_edge.cs"
|
2023-10-08 01:33:46 +08:00
|
|
|
|
[class]{binary_search_edge}-[func]{BinarySearchRightEdge}
|
2023-05-21 19:04:21 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Go"
|
|
|
|
|
|
|
|
|
|
```go title="binary_search_edge.go"
|
|
|
|
|
[class]{}-[func]{binarySearchRightEdge}
|
|
|
|
|
```
|
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
=== "Swift"
|
|
|
|
|
|
|
|
|
|
```swift title="binary_search_edge.swift"
|
|
|
|
|
[class]{}-[func]{binarySearchRightEdge}
|
|
|
|
|
```
|
|
|
|
|
|
2023-07-26 15:35:38 +08:00
|
|
|
|
=== "JS"
|
2023-05-21 19:04:21 +08:00
|
|
|
|
|
|
|
|
|
```javascript title="binary_search_edge.js"
|
|
|
|
|
[class]{}-[func]{binarySearchRightEdge}
|
|
|
|
|
```
|
|
|
|
|
|
2023-07-26 15:35:38 +08:00
|
|
|
|
=== "TS"
|
2023-05-21 19:04:21 +08:00
|
|
|
|
|
|
|
|
|
```typescript title="binary_search_edge.ts"
|
|
|
|
|
[class]{}-[func]{binarySearchRightEdge}
|
|
|
|
|
```
|
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
=== "Dart"
|
2023-05-21 19:04:21 +08:00
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
```dart title="binary_search_edge.dart"
|
2023-05-21 19:04:21 +08:00
|
|
|
|
[class]{}-[func]{binarySearchRightEdge}
|
|
|
|
|
```
|
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
=== "Rust"
|
2023-05-21 19:04:21 +08:00
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
```rust title="binary_search_edge.rs"
|
|
|
|
|
[class]{}-[func]{binary_search_right_edge}
|
2023-05-21 19:04:21 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
=== "C"
|
2023-05-21 19:04:21 +08:00
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
```c title="binary_search_edge.c"
|
2023-05-21 19:04:21 +08:00
|
|
|
|
[class]{}-[func]{binarySearchRightEdge}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Zig"
|
|
|
|
|
|
|
|
|
|
```zig title="binary_search_edge.zig"
|
|
|
|
|
[class]{}-[func]{binarySearchRightEdge}
|
|
|
|
|
```
|
|
|
|
|
|
2023-08-04 05:16:56 +08:00
|
|
|
|
### 转化为查找元素
|
|
|
|
|
|
2023-08-27 23:40:39 +08:00
|
|
|
|
我们知道,当数组不包含 `target` 时,最终 $i$ 和 $j$ 会分别指向首个大于、小于 `target` 的元素。
|
2023-08-04 05:16:56 +08:00
|
|
|
|
|
2023-08-27 22:49:47 +08:00
|
|
|
|
因此,如下图所示,我们可以构造一个数组中不存在的元素,用于查找左右边界。
|
2023-08-04 05:16:56 +08:00
|
|
|
|
|
|
|
|
|
- 查找最左一个 `target` :可以转化为查找 `target - 0.5` ,并返回指针 $i$ 。
|
|
|
|
|
- 查找最右一个 `target` :可以转化为查找 `target + 0.5` ,并返回指针 $j$ 。
|
2023-05-21 19:04:21 +08:00
|
|
|
|
|
2023-08-04 05:16:56 +08:00
|
|
|
|
![将查找边界转化为查找元素](binary_search_edge.assets/binary_search_edge_by_element.png)
|
2023-05-21 19:04:21 +08:00
|
|
|
|
|
2023-08-27 22:49:47 +08:00
|
|
|
|
代码在此省略,值得注意以下两点。
|
2023-05-21 19:04:21 +08:00
|
|
|
|
|
2023-08-20 14:51:39 +08:00
|
|
|
|
- 给定数组不包含小数,这意味着我们无须关心如何处理相等的情况。
|
2023-08-04 05:16:56 +08:00
|
|
|
|
- 因为该方法引入了小数,所以需要将函数中的变量 `target` 改为浮点数类型。
|