Using Arrays.binarySearch, JAVA code


  • -2
    F
    public int search(int[] nums, int target) {
        int i = 0, n = nums.length;
        while (i + 1 < n && nums[i] <= nums[i + 1])
            i++;
        int left = Arrays.binarySearch(nums, 0, i + 1, target);
        int right = Arrays.binarySearch(nums, i + 1, n, target);
        return left < 0 && right < 0 ? -1 : Math.max(left, right);
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.