A concise solution in C++

  • 0
    class Solution {
    public:int search(int A[], int b, int e, int target)
        if(b>e)return -1;
        int mid=(b+e)/2;
        if(A[mid]==target)return mid;
        if(A[e]==target)return e;
            if(target>A[mid]||target<A[e])return search(A,mid+1,e,target);
            else return search(A,b,mid-1,target);
            if(target<A[mid]||target>A[e])return search(A,b,mid-1,target);
            else return search(A,mid+1,e,target);
    int search(int A[], int n, int target) {
        return search(A,0,n-1,target);

Log in to reply

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