A neat c++ solution


  • 0
    W
    void merge(vector<int>& a, int m, vector<int>& b, int n) {
            int p=m-1, q=n-1, k=n+m-1;
            
            while(p>=0 && q>=0) a[k--]=a[p]>b[q]?a[p--]:b[q--];
            for(int i=p; i>=0; i--) a[k--]=a[i];
            for(int i=q; i>=0; i--) a[k--]=b[i];
    }

  • 0
    H

    This a[k--]=a[p]>b[q]?a[p--]:b[q--]; works only when arrays are in ascending order.


Log in to reply
 

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