[leetcode] Search in Rotated Sorted Array II

Search in Rotated Sorted Array II


class Solution {
public:
    int search(int A[], int n, int target) {
        if(n<=0){
            return -1;
        }
        int start=0,end=n;
        while(start!=end){
            int mid=start+(end-start)/2;
            if(target==A[mid]){
                return mid;
            }
            if(A[start]<=A[mid]){
                if(A[start]<=target&&target<A[mid]){
                    end=mid;
                }else{
                    start=mid+1;
                }
            }else{
                if(A[mid]<target&&target<=A[end-1]){
                    start=mid+1;
                }else{
                    end=mid;
                }
            }
        }
        return -1;
    }
};




你可能感兴趣的:([leetcode] Search in Rotated Sorted Array II)