往左边逼近

    public int search(int[] nums, int target) {
        //边界条件判断
        if (nums == null || nums.length == 0)
            return -1;
        int left = 0;
        int right = nums.length - 1;
        while (left < right) {
            int mid = left + (right - left) / 2;
            if (nums[mid] < target) {
                left = mid + 1;
            } else {
                right = mid;
            }
        }
        return nums[left] == target ? left : -1;
    }

查找之后往左边继续查找

    public int search(int[] nums, int target) {
        if (nums == null || nums.length == 0)
            return -1;
        int left = 0;
        int right = nums.length - 1;
        while (left <= right) {
            int mid = left + (right - left) / 2;
            if (nums[mid] < target) {
                left = mid + 1;
            } else if (nums[mid] > target) {
                right = mid - 1;
            } else {
                //找到之后,往左边找
                while (mid != 0 && nums[mid] == nums[mid - 1])
                    mid--;
                return mid;
            }
        }
        return -1;
    }

递归解决

    public int search(int[] nums, int target) {
        if (nums == null || nums.length == 0)
            return -1;
        return helper(nums, target, 0, nums.length - 1);
    }

    private int helper(int[] nums, int target, int left, int right) {
        if (left > right)
            return -1;
        if (nums[left] == target)
            return left;
        int mid = left + (right - left) / 2;
        if (nums[mid] < target) {
            return helper(nums, target, mid + 1, right);
        } else if (nums[mid] > target) {
            return helper(nums, target, left, mid - 1);
        } else {
            return helper(nums, target, left, mid);
        }
    }

我把部分算法题整理成了PDF文档,截止目前总共有900多页,大家可以下载阅读
链接https://pan.baidu.com/s/1hjwK0ZeRxYGB8lIkbKuQgQ
提取码:6666

如果觉得有用就给个赞吧,还可以关注我的《牛客博客》查看更多的详细题解