Given sorted integer Arrays nums1 and nums2, merge nums2 into nums1 as one sorted Array.
Note:
Assume that nums1 have enough space (size that's greater or equal to m + n) to hold a Dditional elements from nums2. The number of elements initialized in nums1 and nums2 is m and n respectively .
1 voidMergeint* Nums1,intMint* NUMS2,intN)2 {3 intI=0;4 if(m==0&&n!=0)5 {6 while(i<N)7 {8nums1[i]=Nums2[i];9i++;Ten } One return ; A } - if(n==0) - return ; the - int*Temparray; - -Temparray= (int*)malloc(sizeof(int) * (m+n)); + - intL=0; + intk1=0, k2=0; A while(k1<m&&k2<N) at { - if(nums1[k1]>=Nums2[k2]) - { -temparray[l++]=nums2[k2++]; - } - Else in { -temparray[l++]=nums1[k1++]; to } + } - the * if(k1==m) $ {Panax Notoginseng while(k2<N) - { thetemparray[l++]=nums2[k2++]; + } A } the + if(k2==N) - { $ while(k1<m) $ { -temparray[l++]=nums1[k1++]; - } the } - Wuyi the - for(i=0; i<m+n;i++) Wu { -nums1[i]=Temparray[i]; About } $ - Free(Temparray); - -}
Leecode-merge Sorted Array