Java using binary search template


  • 0
    F

    The logic is to compare with mid with right pointer then the range will only be in sorted, lower partition.

    public int findMin(int[] nums) {
        if (nums == null || nums.length == 0)  return -1;
        int i = 0, j = nums.length-1;
        while (i+1 < j) {
          int mid = i + (j - i) / 2;
          if (nums[mid] > nums[j]) {
            i = mid;
          } else if (nums[mid] < nums[j]) {
            j = mid;
          } else {
            return nums[mid];
          }
        }
        return Math.min(nums[i], nums[j]);
      }

Log in to reply
 

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