【LeetCode刷题-二分查找】--704.二分查找

704.二分查找

【LeetCode刷题-二分查找】--704.二分查找_第1张图片

class Solution {
    public int search(int[] nums, int target) {
        if(nums.length == 0){
            return -1;
        }
        int left = 0,right = nums.length - 1;
        while(left <= right){
            int mid = (right - left) / 2 + left;
            if(nums[mid] == target){
                return mid;
            }else if(nums[mid] < target){
                left = mid + 1;
            }else{
                right = mid - 1;
            }
        }
        return -1;
    }
}

你可能感兴趣的:(#,二分查找,leetcode,算法)