java:两个有序数组的合并

两个有序数组的合并

/**
     * 将两个有序数组合并为一个有序数组
     * @param arr1
     * @param arr2
     * @return
     */
    public static int[] arrayMerge(int[] arr1, int[] arr2){
        int len1 = arr1.length;
        int len2 = arr2.length;
        int[] result = new int[len1 + len2];
        int l1 = 0, l2 = 0, l3 = 0;
        while (l1 < len1 && l2 < len2){
            if (arr1[l1] < arr2[l2]){
                result[l3++] = arr1[l1++];
            }else {
                result[l3++] = arr2[l2++];
            }
        }
        while (l1 < len1){
            result[l3++] = arr1[l1++];
        }
        while (l2 < len2){
            result[l3++] = arr2[l2++];
        }
        return result;
    }

你可能感兴趣的:(面试,有序数组合并,算法)