码迷,mamicode.com
首页 > 其他好文 > 详细

leetcode--Median of Two Sorted Arrays

时间:2014-10-08 00:27:44      阅读:321      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   io   ar   sp   div   art   

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)%2? a[(n+m)>>1]:(a[(n+m-1)>>1]+a[(n+m)>>1])/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 = 0;  
        int j = 0;  
        int index = 1;  
        int kth;  
        if (m == 0)  
            return b[k-1];  
        if (n == 0)  
            return a[k-1];  
        if (k ==1)  
            return a[0]>b[0] ? b[0] : a[0];  
        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 + 1) / 2);  
        else   // even
            return (findKth(A, m, B, n, total / 2)  
                    + findKth(A, m, B, n, total / 2 + 1)) / 2;  
    }  
};  

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

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

 

leetcode--Median of Two Sorted Arrays

标签:style   blog   http   color   io   ar   sp   div   art   

原文地址:http://www.cnblogs.com/higerzhang/p/4010005.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!