Skip to content

Commit

Permalink
Merge pull request youngyangyang04#2500 from ChainThemAll/fix/0035_rust
Browse files Browse the repository at this point in the history
fix 0035_搜索插入位置 Rust 示例错误
  • Loading branch information
youngyangyang04 authored Apr 30, 2024
2 parents c94895d + e56494b commit b07e850
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions problems/0035.搜索插入位置.md
Original file line number Diff line number Diff line change
Expand Up @@ -313,18 +313,18 @@ func searchInsert(nums []int, target int) int {

```rust
impl Solution {
pub fn search_insert(nums: Vec<i32>, target: i32) -> i32 {
let mut left = 0;
let mut right = nums.len();
while left < right {
pub fn search_insert(nums: Vec<i32>, target: i32) -> i32 {
use std::cmp::Ordering::{Equal, Greater, Less};
let (mut left, mut right) = (0, nums.len() as i32 - 1);
while left <= right {
let mid = (left + right) / 2;
match nums[mid].cmp(&target) {
Ordering::Less => left = mid + 1,
Ordering::Equal => return ((left + right) / 2) as i32,
Ordering::Greater => right = mid,
match nums[mid as usize].cmp(&target) {
Less => left = mid + 1,
Equal => return mid,
Greater => right = mid - 1,
}
}
((left + right) / 2) as i32
right + 1
}
}
```
Expand Down

0 comments on commit b07e850

Please sign in to comment.