81. Search in Rotated Sorted Array II

Posted 鸵鸟

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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;
    }
}

 

以上是关于81. Search in Rotated Sorted Array II的主要内容,如果未能解决你的问题,请参考以下文章

#Leetcode# 81. Search in Rotated Sorted Array II

81. Search in Rotated Sorted Array II

[Leetcode]81. Search in Rotated Sorted Array II

81. Search in Rotated Sorted Array II

81. Search in Rotated Sorted Array II

81. Search in Rotated Sorted Array II