题解 | #二分查找-I#

二分查找-I

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

 * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
 *
 * 
 * @param nums int整型一维数组 
 * @param target int整型 
 * @return int整型
 */
function search( nums ,  target ) {
    // write code here
    let len = nums.length
    if(!len)return -1
    let [left , right] = [0 , len -1]
    while(left <= right){
        let mid = left + Math.floor((right - left) /2)
        let num = nums[mid]
        if(num === target) return mid
        else if(num > target) right = mid - 1
        else left = mid +1
        
    }
    return -1
}
module.exports = {
    search : search
};
全部评论

相关推荐

程序员鼠鼠_春招版:都很烂大街,rpc也基本没人问,考研吧,不然就包装一段实习再去
点赞 评论 收藏
分享
会飞的猿:本人来了,手一抖转错了,我是学生,能还给我吗
点赞 评论 收藏
分享
评论
5
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务