我已经实现了一个可行的函数来完成这个任务,但效率不是很高,因为每次调用都会复制一份新的副本。我尝试将其转换为仅使用a_start、a_end、b_start、b_end,但无法适用于所有情况。请问怎样才能够让它接受a和b数组的起始和结束指针呢?我尝试了以下方法并修改了k-i-1和k-j-1,以便仅使用k,但没有成功。
int m = a_right-a_left, n=b_right-b_left;
int i = (a_left+a_right)/2;
or int i = (int)((m* (k-1)) / (m+n) );
以下是使用每次调用时的新数组副本的工作代码。
public static int kthSmallest(int[] a, int[] b, int k) {
if (a.length==0)
return b[k-1];
else if (b.length==0)
return a[k-1];
else if (b.length<a.length)
return kthSmallest(b, a, k);
// make sure i + j = k - 1
int m = a.length, n=b.length;
int i = (int)((double)m / (m+n) * (k-1)); // make sure i won't be out of bounds
int j = k - 1 - i;
int bj_1 = 0, ai_1 = 0;
if (i==0) { ai_1 = Integer.MIN_VALUE; } // in case i = 0, outOfBound
else { ai_1 = a[i-1]; }
if (j==0) { bj_1 = Integer.MIN_VALUE; } // in case j = 0, outOfBound
else { bj_1 = b[j-1]; }
if (bj_1 < a[i] && a[i] < b[j]) // kth smallest found, b[j-1] < a[i] < b[j]
return a[i];
if (ai_1 < b[j] && b[j] < a[i]) // kth smallest found, a[i-1] < b[j] < a[i]
return b[j];
if ( a[i] < b[j] ) // if true, exclude a's lower bound (if 2 arrays merged, a's lower bound must
// reside before kth smallest, so also update k.
// also exclude b's upper bound, since they are all greater than kth element.
return kthSmallest(Arrays.copyOfRange(a, i+1, a.length), Arrays.copyOfRange(b, 0, j), k-i-1);
else
return kthSmallest(Arrays.copyOfRange(a, 0, i), Arrays.copyOfRange(b, j+1, b.length), k-j-1);
}