题解 | #二分查找-I# | Rust

二分查找-I

https://www.nowcoder.com/practice/d3df40bd23594118b57554129cadf47b

struct Solution{

}

impl Solution {
    fn new() -> Self {
        Solution{}
    }

    /**
    * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
    *
    * 
        * @param nums int整型一维数组 
        * @param target int整型 
        * @return int整型
    */
    pub fn search(&self, nums: Vec<i32>, target: i32) -> i32 {
        let mut low:i32 = 0;
        let mut high = nums.len() as i32 -1;
        while low <= high {
            let mid = low + (high-low)/2;
            if target == nums[mid as usize] {
                return mid;
            } else if target > nums[mid as usize] {
                low = mid + 1;
            } else {
                high = mid - 1;
            }
        }
        return -1;
    }
}

全部评论

相关推荐

身边有人上海、深圳&nbsp;6、7k&nbsp;都去了,真就带薪上班了。
程序员小白条:木的办法, 以后越来越差,还是家附近宅着吧,毕业的人越来越多,岗位都提供不出来,经济又过了人口红利期
点赞 评论 收藏
分享
07-01 13:37
门头沟学院 Java
steelhead:不是你的问题,这是社会的问题。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务