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.
水题,直接把B中元素加入A中,再排序即可。
利用Arrays.sort(array)进行快速排列。
import java.util.Arrays;
public class Solution {
public void merge(int A[], int m, int B[], int n) {
for(int i=m,j=0;j<n;i++,j++)
A[i]=B[j];
Arrays.sort(A);
}
}
本文介绍了一种简单的方法来合并两个已排序的整数数组,并通过Java内置的Arrays.sort()方法进行排序,确保最终数组仍保持有序状态。此方法适用于数组A有足够的空间容纳来自数组B的额外元素。
324

被折叠的 条评论
为什么被折叠?



