81. Search in Rotated Sorted Array II

class Solution {
    public boolean search(int[] nums, int target) {
        if(nums.length==0)
            return false;
        int lo=0;
        int hi=nums.length-1;
        while(lo<hi)
        {
            int mid=(lo+hi)/2;
            if(nums[mid]==target)
                return true;
            if(nums[mid]>nums[hi])
            {
                if(nums[lo]<=target&&target<nums[mid])
                    hi=mid;
                else
                    lo=mid+1;
            }
            else if(nums[mid]<nums[hi])
            {
                if(nums[mid]<target&&target<=nums[hi])
                    lo=mid+1;
                else
                    hi=mid;
            }
            else
                hi--;
        }
        return nums[lo]==target;
    }
}

 

posted @ 2017-09-27 11:10  Weiyu Wang  阅读(120)  评论(0编辑  收藏  举报