704. 二分查找

class Solution {
    public int search(int[] nums, int target) {
        int left = 0, right = nums.length, middle;

        while (left < right) {
            middle = left + ((right - left) >> 1);

            if (target < nums[middle]) {
                right = middle;
            } else if (target > nums[middle]) {
                left = middle + 1;
            } else {
                System.gc();
                return middle;
            }
        }

        return -1;
    }
}
image.png

你可能感兴趣的:(704. 二分查找)