File tree Expand file tree Collapse file tree 1 file changed +9
-9
lines changed Expand file tree Collapse file tree 1 file changed +9
-9
lines changed Original file line number Diff line number Diff line change @@ -313,18 +313,18 @@ func searchInsert(nums []int, target int) int {
313
313
314
314
``` rust
315
315
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 {
320
320
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 ,
325
325
}
326
326
}
327
- (( left + right ) / 2 ) as i32
327
+ right + 1
328
328
}
329
329
}
330
330
```
You can’t perform that action at this time.
0 commit comments