How about this one


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

    }


Log in to reply
 

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