二分查找

正常实现

	public int binarySearch(int[] nums, int key) {
		int l=0,h=nums.length-1;
		while(l<=h){
			int m=l+(h-l)/2;
			if(nums[m]==key){
				return m;
			}else if(nums[m]<key){
				l=m+1;
			}else{
				h=m-1;
			}
		}
		return -1;
	}

69 x 的平方根

    public int mySqrt(int x) {
        if(x<=1)
            return x;
        int l=1,h=x;
        while(l<=h){
            int m=l+(h-l)/2;
            int sqrt=x/m;
            if(m==sqrt){
                return m;
            }else if(m<sqrt){
                l=m+1;
            }else{
                h=m-1;
            }
        }
        return h;
    }

牛顿法,令 f ( x ) = x 2 − t f(x)=x^2-t f(x)=x2t

    public int mySqrt(int x) {
        if(x<=1)
            return x;
        long r=x;
        while(r>x/r){
            r=(r+x/r)/2;
        }
        return (int)r;
    }

744 寻找比目标字母大的最小字母

    public char nextGreatestLetter(char[] letters, char target) {
        int l=0,h=letters.length-1;
        if(letters[h]<=target)
            return letters[0];
        while(l<=h){
            int m=l+(h-l)/2;
            if(letters[m]<=target){
                l=m+1;
            }else{
                h=m-1;
            }
        }
        return letters[l];
    }

540 有序数组中的单一元素

    public int singleNonDuplicate(int[] nums) {
        if(nums==null || nums.length==0)
            return -1;
        int l=0,h=nums.length-1;
        while(l<h){
            int m=l+(h-l)/2;
            if(m%2==1){
                m--;// 保证 l/h/m 都在偶数位,使得查找区间大小一直都是奇数
            }
            if(nums[m]==nums[m+1]){
                l=m+2;
            }else if(m==0 || nums[m]!=nums[m-1]){
                return nums[m];
            }else{
                h=m-2;
            }
        }
        return nums[l];
    }

278 第一个错误的版本

即等同于找到第一个等于某个值,注意与查找第一个大于某个数字的区别。

  • nums[m] > nums[h]            l=m+1
  • nums[m] <= nums[h]          h=m-1 //终止条件为l <= h
    public int firstBadVersion(int n) {
        if(n<=0)
            return -1;
        int l=1,h=n;
        while(l<=h){
            int m=l+(h-l)/2;
            if(isBadVersion(m)){               
                h=m-1;
            }else{
                l=m+1;
            }
        }
        return l;
    }

153 寻找旋转排序数组中的最小值

  • nums[m] > nums[h]            l=m+1
  • nums[m] <= nums[h]          h=m //终止条件为l < h
    public int findMin(int[] nums) {
        if(nums==null || nums.length==0)
            return -1;
        int l=0,h=nums.length-1;
        while(l<h){            
            int m=l+(h-l)/2;
            if(nums[m]>nums[h]){
                l=m+1;
            }else{
                h=m;//此时nums[m]可能为最小的时
            }
        }
        return nums[l];
    }

154. 寻找旋转排序数组中的最小值 II

主要是解决碰到[2,2,2,0,2]和[2,0,2,2,2]这种情况时该怎么移动指针的问题。
还是和最右边的元素比较:

  • nums[m] > nums[h]            l=m+1
  • nums[m] < nums[h]            h=m //终止条件为l < h
  • 其他                                    h=h-1
    public int findMin(int[] nums) {
        int l=0, h= nums.length-1;
        while(l < h){
            int m = (l + h)/2; 
            if(nums[m] > nums[h]){
                l = m +1;
            }else if(nums[m] < nums[h]){
                h = m;
            }else{
                h = h - 1; 
            }
        }
        return nums[l];
    }

34 在排序数组中查找元素的第一个和最后一个位置

可以看作是求被查找元素的第一个位置和被查找元素+1的第一个位置

    public int[] searchRange(int[] nums, int target) {
        int first=binarySearch(nums,target);
        int last=binarySearch(nums,target+1)-1;
        if(first==nums.length||nums[first]!=target){
            return new int[]{-1,-1};
        }else{
            return new int[]{first,last};
        }
            
    }
    //查找第一个等于target的位置
    public int binarySearch(int []nums,int target){
        int l = 0, h = nums.length - 1;
	    while (l <= h) {
            int m = l + (h - l) / 2;
            if (nums[m] >=target) {
	            h = m-1;
	        } else if(nums[m]<target) {
	            l = m + 1;
	        }
	    }
	    return l;//若不存在,则可能返回0(nums={1,2},target=0)或nums.length(nums={1,2},target=3}或第一个大于target的值
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值