合并俩个有序的数组

85 阅读1分钟

合并俩个数组为有序数组

截屏2022-03-10上午10.27.55.png

    public static void main(String[] args) {
        int[] a = {1, 3, 5, 7, 9};
        int[] b = {2, 4, 6, 8};
        int[] k = new int[a.length + b.length];
        int i = a.length - 1;
        int j = b.length - 1;
        int z = k.length - 1;
        while (i >= 0) {
            k[z--] = j>=0 && a[i] < b[j] ?  b[j--] :a[i--] ;
        }
        Arrays.stream(k).forEach(ste->{
            System.out.println(ste);
        });

    }
}