代码语言:javascript复制分享一个面试高频算法题,将两个有序数组,进行合并。
public class Main {
public static void main(String[] args) {
int[] a = {1, 3, 5, 7, 9, 11, 12};
int[] b = {2, 3, 4, 6, 8, 10, 33};
int[] c = new int[a.length b.length];
int i = 0, j = 0, k = 0;
// a,b数组都有元素时
while (i < a.length && j < b.length) {
if (a[i] < b[j]) {
c[k ] = a[i ];
} else {
c[k ] = b[j ];
}
}
// 若a有剩余
while (i < a.length) {
c[k ] = a[i ];
}
// 若b有剩余
while (j < b.length) {
c[k ] = b[j ];
}
// 遍历输出
Arrays.stream(c).forEach(s -> System.out.println("s = " s));
}
}