[leetCode]Merge Sorted Array
并归排序的思想,新get一种地址取法~之前怎么想不到。。
1 #include <iostream> 2 #include <string.h> 3 using namespace std; 4 5 class Solution { 6 public: 7 void merge(int A[], int m, int B[], int n) { 8 if(n == 0) return ; 9 if(m == 0){ 10 memcpy(A,B,sizeof(int)*n); 11 return ; 12 } 13 int *num = new int[m+n]; 14 memset(num,0,m+n); 15 int im,in,index; 16 im = in = index =0; 17 while(index<m+n){ 18 num[index++] = (A[im] <= B[in])?(A[im++]):(B[in++]); 19 if(in == n || im == m) break; 20 } 21 if(in == n) memcpy(&num[index],&A[im],sizeof(int)*(m-im));//new skill gets! 22 else memcpy(&num[index],&B[in],sizeof(int)*(n-in)); 23 memcpy(A,num,sizeof(int)*(m+n)); 24 delete []num; 25 } 26 };
艰难的成长