首页 > 代码库 > LeetCode Merge Sorted Array

LeetCode Merge Sorted Array

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 arem 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);
    }
}


LeetCode Merge Sorted Array