Easy Java Solution


  • 0
    Y
    public class Solution {
        public boolean search(int[] nums, int target) {
            int left = 0;
            int right = nums.length - 1;
            int mid = 0;
            while(left < right){
                mid = left + (right - left) / 2;
                if(nums[mid] == target)
                    return true;
                else if(nums[mid] == nums[left])
                    left++;
                else if(nums[mid] > nums[left]){
                    if(target > nums[mid] || target < nums[left])
                        left = mid + 1;
                    else
                        right = mid - 1;
                }else{
                    if(target > nums[right] || target < nums[mid])
                        right = mid - 1;
                    else
                        left = mid + 1;
                }
            }
            
            return nums[left] == target;
        }
    }

Log in to reply
 

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