mirror of
https://github.com/krahets/hello-algo.git
synced 2024-12-26 02:26:28 +08:00
build
This commit is contained in:
parent
10c61fd528
commit
e979c6d1ff
1 changed files with 4 additions and 4 deletions
|
@ -227,8 +227,8 @@ comments: true
|
|||
=== "Rust"
|
||||
|
||||
```rust title="binary_search_insertion.rs"
|
||||
/* 二分查找插入点(存在重复元素) */
|
||||
pub fn binary_search_insertion(nums: &[i32], target: i32) -> i32 {
|
||||
/* 二分查找插入点(无重复元素) */
|
||||
fn binary_search_insertion_simple(nums: &[i32], target: i32) -> i32 {
|
||||
let (mut i, mut j) = (0, nums.len() as i32 - 1); // 初始化双闭区间 [0, n-1]
|
||||
while i <= j {
|
||||
let m = i + (j - i) / 2; // 计算中点索引 m
|
||||
|
@ -237,10 +237,10 @@ comments: true
|
|||
} else if nums[m as usize] > target {
|
||||
j = m - 1; // target 在区间 [i, m-1] 中
|
||||
} else {
|
||||
j = m - 1; // 首个小于 target 的元素在区间 [i, m-1] 中
|
||||
return m;
|
||||
}
|
||||
}
|
||||
// 返回插入点 i
|
||||
// 未找到 target ,返回插入点 i
|
||||
i
|
||||
}
|
||||
```
|
||||
|
|
Loading…
Reference in a new issue