A simple C solution[Accepted]


  • 0
    B
    void merge(int* nums1, int m, int* nums2, int n) {
        if(n==0){return ;}
        int *ret=(int*)malloc(sizeof(int)*(m+n));
        int count1=0;
        int count2=0;
        int count3=0;
        while(count1!=m&&count2!=n){
            ret[count3++]=nums1[count1]<nums2[count2]?nums1[count1++]:nums2[count2++];
        }
        while(count1!=m){
             ret[count3++]=nums1[count1++];
         }
         while(count2!=n){
             ret[count3++]=nums2[count2++];
         }
        for(int i=0;i<m+n;i++){
            nums1[i]=ret[i];
        }
    }
    

Log in to reply
 

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