题解 | #二分查找-I#

二分查找-I

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

 public int search (int[] nums, int target) {
        // write code here
        int start  = 0;
        int end = nums.length-1;
        while(start<=end){
            if(target == nums[(start+end)/2]){
                return (start+end)/2;
            }else if(target > nums[(start+end)/2]){
                        start =((start+end)/2)+1;
                        continue;
            }else if(target < nums[(start+end)/2]){
                end = ((start+end)/2)-1;
                continue;
            }
        }
        return -1;
    }

//while循环的时候 记住是 start<=end 防止漏掉数组为[1,2]的情况

全部评论

相关推荐

09-29 07:57
已编辑
门头沟学院 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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