Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.
Note:
You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold additional elements from nums2. The number of elements initialized in nums1 and nums2 are m and n respectively.
1 void merge(int* nums1, int m, int* nums2, int n) 2 { 3 int i=0; 4 if(m==0&&n!=0) 5 { 6 while(i<n) 7 { 8 nums1[i]=nums2[i]; 9 i++; 10 } 11 return ; 12 } 13 if(n==0) 14 return ; 15 16 int *TempArray; 17 18 TempArray=(int*)malloc(sizeof(int)*(m+n)); 19 20 int L=0; 21 int k1=0,k2=0; 22 while(k1<m&&k2<n) 23 { 24 if(nums1[k1]>=nums2[k2]) 25 { 26 TempArray[L++]=nums2[k2++]; 27 } 28 else 29 { 30 TempArray[L++]=nums1[k1++]; 31 } 32 } 33 34 35 if(k1==m) 36 { 37 while(k2<n) 38 { 39 TempArray[L++]=nums2[k2++]; 40 } 41 } 42 43 if(k2==n) 44 { 45 while(k1<m) 46 { 47 TempArray[L++]=nums1[k1++]; 48 } 49 } 50 51 52 53 for(i=0;i<m+n;i++) 54 { 55 nums1[i]=TempArray[i]; 56 } 57 58 free(TempArray); 59 60 }