合并两个有序的数组
![](https://img2022.cnblogs.com/blog/608798/202211/608798-20221112154152970-897796297.png)
import java.util.*;
public class Solution {
public void merge(int A[], int m, int B[], int n) {
//新建数组C,比较数组A和B的元素,把最小放入C中
//最小的数字所在数组指针后移动
int[] C = new int[m + n];
for (int i = 0, j = 0, k = 0; i < m || j < n;) {
if (k == m + n) {
break;
}
if(i<m && j<n){
if (A[i] < B[j]) {
C[k] = A[i];
i++;
k++;
} else {
C[k] = B[j];
j++;
k++;
}
}else{
if (i == m) {
C[k] = B[j];
j++;
k++;
continue;
}
if (j == n) {
C[k] = A[i];
i++;
k++;
continue;
}
}
}
for(int i=0; i<m+n; i++){
A[i] = C[i];
}
}
}