mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-07 15:45:40 +08:00
fix 0035_搜索插入位置 Rust 示例错误
This commit is contained in:
@ -314,17 +314,17 @@ func searchInsert(nums []int, target int) int {
|
|||||||
```rust
|
```rust
|
||||||
impl Solution {
|
impl Solution {
|
||||||
pub fn search_insert(nums: Vec<i32>, target: i32) -> i32 {
|
pub fn search_insert(nums: Vec<i32>, target: i32) -> i32 {
|
||||||
let mut left = 0;
|
use std::cmp::Ordering::{Equal, Greater, Less};
|
||||||
let mut right = nums.len();
|
let (mut left, mut right) = (0, nums.len() as i32 - 1);
|
||||||
while left < right {
|
while left <= right {
|
||||||
let mid = (left + right) / 2;
|
let mid = (left + right) / 2;
|
||||||
match nums[mid].cmp(&target) {
|
match nums[mid as usize].cmp(&target) {
|
||||||
Ordering::Less => left = mid + 1,
|
Less => left = mid + 1,
|
||||||
Ordering::Equal => return ((left + right) / 2) as i32,
|
Equal => return mid,
|
||||||
Ordering::Greater => right = mid,
|
Greater => right = mid - 1,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
((left + right) / 2) as i32
|
right + 1
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
Reference in New Issue
Block a user