标签:interview
Option 1.
Create an extra big array C.
iterate both A and B.
return C.
O(m + n)
Option 2.
Assume A is big enough.
Iterate A and B starting from end.
Put max value to the end of A.
int[] merge (int[] a, int[] b, int i) { // assume a[x] has value when x <= i; // And a.length = b.length + i; int m = b.length - 1; int n = i; int k = a.length - 1; for ( k >= 0 ) { if (m < 0) { a[k] = b[n]; n --; } else if (n < 0) { a[k] = a[m]; m --; } else if (a[m] >= b[n]) { a[k] = a[m]; m --; } else { a[k] = b[n]; n --; } k --; } }
标签:interview
原文地址:http://7371901.blog.51cto.com/7361901/1586150