Given two sorted integer arrays A and B, merge B into A as one sorted array.
Note:
You may assume that A has enough space (size that is greater or equal to m + n) to hold additional elements from B. The number of elements initialized in A and B are m andn respectively.
思路:用直接插入排序法
public class Solution {
public void merge(int A[], int m, int B[], int n) {
for (int i = 0; i < n; i++) {
int j = m - 1 + i;
while (j >= 0 && A[j] > B[i]) {
A[j + 1] = A[j];
j--;
}
A[j + 1] = B[i];
}
}
}