题解 | #合并两个有序的数组#
合并两个有序的数组
http://www.nowcoder.com/practice/89865d4375634fc484f3a24b7fe65665
*
* @param A int整型一维数组
* @param ALen int A数组长度
* @param B int整型一维数组
* @param BLen int B数组长度
* @return void
*
* C语言声明定义全局变量请加上static,防止重复定义
*/
void merge(int* A, int ALen, int m, int* B, int BLen, int n) {
// write code here
int* C = (int*)malloc(sizeof(int) * m);
for(int i = 0; i < m; i++){
C[i] = A[i];
}
int j = 0, k = 0, i;
for(i = 0; i < m+n && k < n && j < m; i++){
if(C[j] <= B[k]){
A[i] = C[j++];
}else{
A[i] = B[k++];
}
}
while(j < m){
A[i++] = C[j++];
}
while(k < n){
A[i++] = B[k++];
}
}