GeniusOfCX

导航

leetcode 35 rust

题目

代码

pub fn search_insert(nums: Vec<i32>, target: i32) -> i32 {
    let mut left = 0;
    let mut right = nums.len()-1;
    let mut mid;
    while left <= right {
        mid = left + ((right - left) >> 1);
        if mid >= nums.len() {
            break;
        }
        match nums[mid].cmp(&target){
            std::cmp::Ordering::Less => left = mid + 1,
            std::cmp::Ordering::Greater => right = mid -1,
            std::cmp::Ordering::Equal => return mid as i32,
        }
    }
    left as i32
}

posted on 2021-04-08 13:48  GeniusOfCX  阅读(31)  评论(0编辑  收藏  举报