Could someone help me please.


  • 0
    M

    int search(int A[], int n, int target) {

        if(!n)
        return -1;
        
        if(n==1)
        return A[0]==target? 0:-1;
        
        int middle=0,start=0, end=n-1;
        while(start<end)
        {
            middle=(start+end)/2;
            
            if(A[middle]==target)
            return middle;
            
            else if (A[start]<=A[middle])
            {
                if(target>=A[start] && target<A[middle])
                end=middle-1;
                else
                start=middle+1;
            }
            
            else  //if(A[start]>A[middle])
            {
                if( target>A[middle] && target<=A[end])
                start=middle+1;
                else
                end=middle-1;
            }
        }
        return -1;
    }
    

    how to fix it ?


  • 0
    C

    how about change the judgement of while loop to :
    while(start<=end)


Log in to reply
 

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