牛客网 | 有序数组合并

有两个从小到大排序以后的数组A和B,其中A的末端有足够的缓冲空容纳B。请编写一个方法,将B合并入A并排序。

给定两个有序int数组AB,A中的缓冲空用0填充,同时给定A和B的真实大小int n和int m,请返回合并后的数组。

public class Merge {
    public int[] mergeAB(int[] A, int[] B, int n, int m) {
        // write code here
        int i = n-1;
        int j = m-1;
        int k = n+m-1;
        while(i>=0&&j>=0)
        {
            if(A[i]>B[j])
                A[k--] = A[i--];
            else
                A[k--] = B[j--];
             
             
        }
        while(i>=0)
            A[k--] = A[i--];
        while(j>=0)
            A[k--] = B[j--];
        return A;
    }
}


你可能感兴趣的:(java)