二分查找

二分查找-II

https://www.nowcoder.com/practice/4f470d1d3b734f8aaf2afb014185b395?tpId=188&&tqId=38588&rp=1&ru=/activity/oj&qru=/ta/job-code-high-week/question-ranking

function search( nums ,  target ) {
    let start=0;
    let end=nums.length-1;
    let index=-1;
    while(start<=end){
        let middle=Math.floor((start+end)/2);

        if(nums[middle]==target){
            index=middle;
            end=middle-1;
        }

        if(nums[middle]<target){

            start=middle+1;

        }else{
            end=middle-1;
        }



    }

    return index
}
全部评论

相关推荐

点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务