劍指offer{面試題4附加:有兩個排序的陣列A1和A2 }
阿新 • • 發佈:2018-12-14
//思路:如出一轍,開闢新記憶體,進行比較 如果有一方為空了,另一方整體加入
public class test04_1 { public static int fun(int[] a,int[] b,int len1,int len2) { int c = len1+len2; while (len1>=0&&len2>=0) { if (a[len1-1]>=b[len2-1]) { a[c-1] = a[len1-2]; c--; } else { a[c-1] = a[len2-2]; c--; } //有一方減完了 while (len1>=0) { a[c-1] = a[len1-2]; c--; } while(len2>=0) { a[c-1] = b[len1-2]; c--; } } } }