题目描述:
合并两个排序的整数数组A和B变成一个新的数组。
eg.
给出A=[1,2,3,4],B=[2,4,5,6],返回 [1,2,2,3,4,4,5,6]
/**
* @param A
* and B: sorted integer array A and B.
* @return: A new sorted integer array
*/
public int[] mergeSortedArray(int[] A, int[] B) {
int lengthA = A.length;
int lengthB = B.length;
int[] array = new int[lengthA + lengthB];
int i = 0, j = 0, k = 0;
while (j < lengthA && k < lengthB) {
if (A[j] < B[k]) {
array[i++] = A[j++];
} else {
array[i++] = B[k++];
}
}
while (j < lengthA) {
array[i++] = A[j++];
}
while (k < lengthB) {
array[i++] = B[k++];
}
return array;
}