Java Solution - 33. Search in Rotated Sorted Array


  • 1
    L
    class Solution {
        public int search(int[] nums, int target) {
            if(nums.length == 0) return -1;
            int l = 0, r = nums.length-1;
            while(l <= r){
                int m = (l+r)/2;
                if(nums[m] == target){
                    return m;
                }else if(nums[m]<nums[r]){
                    if(nums[m] < target && nums[r] >= target) l = m+1;
                    else r = m-1;
                }else{
                    if(nums[l] <= target && nums[m] > target) r = m-1;
                    else l = m+1;
                }
            }
            return -1;
        }
    }
    

Log in to reply
 

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