Can anyone tell me why "exceed time limitation"?


  • 0
    H

    I merge A and B without extra space, and the time complexity is O(m+n)

    public void merge(int A[], int m, int B[], int n) {
       int i=m-1;
       int j=n-1;
       int k=m+n-1;
       while(k>=0){
           if(m==0){
               A[k]=B[j];
               j--;
               k--;
           }else if(n==0){
               return;
           }
               else if(i>=0&&j>=0){
               if(A[i]>=B[j]){
                   A[k]=A[i];
                   i--;
                   k--;
               }
               if(A[i]<B[j]){
                   A[k]=B[j];
                   j--;
                   k--;
               }
           }
           
       }
    

    }


Log in to reply
 

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