mirror of
https://github.com/krahets/hello-algo.git
synced 2024-12-27 03:16:29 +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"
|
||||||
|
|
||||||
```rust title="binary_search_insertion.rs"
|
```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]
|
let (mut i, mut j) = (0, nums.len() as i32 - 1); // 初始化双闭区间 [0, n-1]
|
||||||
while i <= j {
|
while i <= j {
|
||||||
let m = i + (j - i) / 2; // 计算中点索引 m
|
let m = i + (j - i) / 2; // 计算中点索引 m
|
||||||
|
@ -237,10 +237,10 @@ comments: true
|
||||||
} else if nums[m as usize] > target {
|
} else if nums[m as usize] > target {
|
||||||
j = m - 1; // target 在区间 [i, m-1] 中
|
j = m - 1; // target 在区间 [i, m-1] 中
|
||||||
} else {
|
} else {
|
||||||
j = m - 1; // 首个小于 target 的元素在区间 [i, m-1] 中
|
return m;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// 返回插入点 i
|
// 未找到 target ,返回插入点 i
|
||||||
i
|
i
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
Loading…
Reference in a new issue