Skip to content

Commit b07e850

Browse files
Merge pull request youngyangyang04#2500 from ChainThemAll/fix/0035_rust
fix 0035_搜索插入位置 Rust 示例错误
2 parents c94895d + e56494b commit b07e850

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

problems/0035.搜索插入位置.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -313,18 +313,18 @@ func searchInsert(nums []int, target int) int {
313313

314314
```rust
315315
impl Solution {
316-
pub fn search_insert(nums: Vec<i32>, target: i32) -> i32 {
317-
let mut left = 0;
318-
let mut right = nums.len();
319-
while left < right {
316+
pub fn search_insert(nums: Vec<i32>, target: i32) -> i32 {
317+
use std::cmp::Ordering::{Equal, Greater, Less};
318+
let (mut left, mut right) = (0, nums.len() as i32 - 1);
319+
while left <= right {
320320
let mid = (left + right) / 2;
321-
match nums[mid].cmp(&target) {
322-
Ordering::Less => left = mid + 1,
323-
Ordering::Equal => return ((left + right) / 2) as i32,
324-
Ordering::Greater => right = mid,
321+
match nums[mid as usize].cmp(&target) {
322+
Less => left = mid + 1,
323+
Equal => return mid,
324+
Greater => right = mid - 1,
325325
}
326326
}
327-
((left + right) / 2) as i32
327+
right + 1
328328
}
329329
}
330330
```

0 commit comments

Comments
 (0)