题目链接:merge-sorted-array
import java.util.Arrays;
/**
*
* 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 and n respectively.
*
*/
public class MergeSortedArray {
static void merge(int A[], int m, int B[], int n) {
int p = m - 1, q = n - 1;
int count = m + n - 1;
while(p >= 0 && q >= 0) {
A[count--] = (A[p] < B[q]) ? B[q--] : A[p--];
}
while(q >= 0) A[count--] = B[q--];
// System.out.println(Arrays.toString(A));
}
public static void main(String[] args) {
merge(new int[]{1, 2 ,3, 4, 5, 0, 0, 0, 0 ,0}, 2, new int[]{6, 7, 8, 9, 10}, 5);
}
}