leetcode第二题--Median of Two Sorted Arrays

时间:2023-03-09 23:10:47
leetcode第二题--Median of Two Sorted Arrays

Problem:There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).

首先要知道什么是median,也就是我们所知的中位数,处于中间位置的数,很简单,如果长度为奇数,那么中位数就是中间的数,如,1,2,4,那么中位数就是2。如果长度为偶数,那就是中间两个数的均值,如,1,2,3,4,那么中位数就是2.5。

Suool在http://blog.csdn.net/suool/article/details/38343457中提到的第一种方法如下:

合并,排序,找中间位置的值就好:

class Solution {
public:
double findMedianSortedArrays(int A[], int m, int B[], int n) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int *a=new int[m+n]; memcpy(a,A,sizeof(int)*m);
memcpy(a+m,B,sizeof(int)*n); sort(a,a+n+m); double median=(double) ((n+m)%? a[(n+m)>>]:(a[(n+m-)>>]+a[(n+m)>>])/2.0); delete a; return median;
}
};

但是我们可以看到中间有个排序sort(a,a+n+m)这个复杂度已经是超过要求了。但是奇怪的是在leetcode上测试居然通过了。

还有一种方法在上述链接中也提到了,就是先在两个数组中找第k个值,然后根据奇数偶数确定k就好。但是该链接上给出的代码是错误了,通过不了。

我找到了原因,把正确的贴出如下:

class Solution
{
private:
double findKth(int a[], int m, int b[], int n, int k)
{ //find kth small
int i = ;
int j = ;
int index = ;
int kth;
if (m == )
return b[k-];
if (n == )
return a[k-];
if (k ==)
return a[]>b[] ? b[] : a[];
while(index <= k && i < m && j < n)
{
if( a[i] >= b[j])
{
index ++;
kth = b[j];
j ++;
}
else
{
index ++;
kth = a[i];
i ++;
}
}
if( index <= k && j == n) // must be <= can't be only <
{
kth = a[i+k-index];
}
if (index <= k && i == m)
kth = b[j+k-index];
return kth;
}
public:
double findMedianSortedArrays(int A[], int m, int B[], int n)
{
int total = m + n;
if (total & 0x1) // odd
return findKth(A, m, B, n, (total + ) / );
else // even
return (findKth(A, m, B, n, total / )
+ findKth(A, m, B, n, total / + )) / ;
}
};

原链接的代码在线测试时会出现本来是100000.5的却错误输出为100000.25,原因是原作者在k==2的时候假设错误了,并且在之后的index中没有考虑到index到了k值但是还没有达到第k小,要用小于等于号,而不是小于号,详见上述代码中注释。修改后就通过了。

暂且知道这两种方法,暂不深究了,继续前行