将多个数组统一为一个排序数组的最佳方法

Best way to unifies several arrays into one sorted array

以下内容摘自求职面试:

For an array of sorted arrays, write a function that unifies the arrays into one sorted array.

我想过使用一个HashSet<E>,并为每个数组添加一个完整的数组,一个订单(我不知道,但它必须是一个预先编写的方法吗?! ),但我可以发誓有一个简单的解决方案...

有什么建议吗?

谢谢!

您基本上是想从合并排序算法中窃取一种方法,该算法组合了两个(可以扩展到更多)数组。这是一个示例(用于合并 2 个数组):

private static int[] merge(int[] left, int[] right) {
    int lengthResult = left.length + right.length;
    int[] result = new int[lengthResult];
    int indexL=0, indexR=0, indexResult = 0;

    //while there are elements left in left or right
    while(indexL < left.length || indexR < right.length){

        //BOTH left and right still have elements 
        if(indexL < left.length && indexR < right.length){
            //if the left item is greater than right item
            if(left[indexL] <= right[indexR]){
                result[indexResult] = left[indexL];
                indexL++;
                indexResult++;
            }else{
                result[indexResult] = right[indexR];
                indexR++;
                indexResult++;
            }
        //means only left OR right have elements left
            //see if left has stuff
        }else if(indexL < left.length){
            result[indexResult] = left[indexL];
            indexL++;
            indexResult++;
        }else if(indexR < right.length){
            result[indexResult] = right[indexR];
            indexR++;
            indexResult++;
        }
    }
    return result;
}