解题思路
现省略,具体后边再写,只要熟悉归并算法就不太难。代码
package FileTest;
public class Test {
public static int[] arrayMerge(int[] a, int[] b) {
int[] am = new int[a.length + b.length];
int ai = 0;
int bj = 0;
while ((ai<a.length) && (bj<b.length)) {
if(a[ai] < b[bj]) {
am[ai + bj] = a[ai];
ai++;
} else {
am[ai + bj] = b[bj];
bj++;
}
}
while (ai < a.length) {
am[ai + bj] = a[ai];
ai++;
}
while (bj < b.length) {
am[ai + bj] = b[bj];
bj++;
}
return am;
}
public static void main(String[] args) {
int[] a = {0,1,2,3,4,5,6,6};
int[] b = {1,2,3,4,5,6,7,8,9};
int[] am = new int[a.length + b.length];
am = arrayMerge(a, b);
for (int i=0;i < am.length;i++) {
System.out.print(am[i] + " ");
}
}
}