File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -313,18 +313,18 @@ func searchInsert(nums []int, target int) int {
313313
314314``` rust
315315impl 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```
You can’t perform that action at this time.
0 commit comments