6m log2n


  • 0
    P

    class Solution {
    public:
    bool search(vector<int>& nums, int target) {
    if(nums.empty()) return -1;
    int n=nums.size();
    if(n==1) return nums[0]==target?true:false;
    int pivot=0;
    for(int i=1;i<n;++i){
    if(nums[i]<nums[i-1]) {pivot=i;break;};
    }

        int mid,midI;
        int start = 0,end=n-1;
        while(start<=end){
            mid=(start+end)/2;
            midI=(mid+pivot)%n;
            if(target==nums[midI]) return true;
            if(target>nums[midI]) start=(mid+1);
            if(target<nums[midI]) end=(mid-1);
        }
    
             return false;
       
    }   
    

    };


Log in to reply
 

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