1ms Java solution short and clean with explanation


  • 0
    S
    public boolean search(int[] nums, int target) {
       int min = 0;
       // find min value's index
       for (int i = 1; i < nums.length; i++) 
          if (nums[i] < nums[i-1]){
             min = i;
             break;
          }
       
       // Binary search start from min index to the max
       int n = nums.length, l = min, r = n-1+min;
       while (l<=r) {
          int m = (l+r)/2;
          if (target == nums[m%n]) return true;
          else if (target > nums[m%n]) l = m+1;
          else r = m-1;
       }
       return false;
    }
    

Log in to reply
 

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