5 lines C solution; 0ms


  • 0
    Z
    void merge(int* m, int mS, int* n, int nS) {
      //if(nS <=0) return; // no need to merge
    
      int idx1=mS-1, idx2=nS-1;
    
      while(idx1>=0 && idx2>=0)
        if(m[idx1]>n[idx2])    m[idx1+idx2+1] = m[idx1--];
        else                   m[idx1+idx2+1] = n[idx2--];
    
      while(idx2>=0)    m[idx2] = n[idx2--];
      
      // no need to loop through idx1>=0
    }

Log in to reply
 

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