[LeetCode] Median of Two Sorted Arrays [16]

題目

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)).

原題鏈接(點我)

解題思路

返回兩個排序數組的中位數。這個題可以有以下幾個思路:
首先可以想到的是將兩個數組merge起來,然後返回其中位數。
第二個是,類似merge的思想加上計數,找到(m+n)/2個數或者其前後的數,這個就可以算出中位數。這個方法對於各種情況需要一一考慮到。
第三個,假設A[k/2-1]<B[k/2-1],那麼A[k/2-1]之前的數一定在整個有序數列中(m+n)/2之前。
這裏我給出後面兩種思路的代碼。

代碼實現

代碼一(思路三)

class Solution {
public:
    double findMedianSortedArrays(int A[], int m, int B[], int n) {
        int len = m+n;
        if(len & 0x1){
            return findkth(A, m, B, n, len/2 +1);
        }else{
            return ( findkth(A, m, B, n, len/2) + findkth(A, m, B, n, len/2+1) )/2.0;
        }
    }
    
    double findkth(int A[], int m, int B[], int n, int k){
        if(m>n) return findkth(B, n, A, m, k);
        if(m==0) return B[k-1];
        if(k==1) return min(A[0], B[0]);
        int Aindex = min(m, k/2);
        int Bindex = k-Aindex;
        if(A[Aindex-1] < B[Bindex-1]){
            return findkth(A+Aindex, m-Aindex, B, n, k-Aindex);
        }
        else if(A[Aindex-1] > B[Bindex-1]){
            return findkth(A, m, B+Bindex, n-Bindex, k-Bindex);
        }
        else{
            return A[Aindex-1];
        }
    }
};
代碼二(思路二--計數)
class Solution {
public:
    double findMedianSortedArrays(int A[], int m, int B[], int n) {
        int ret = -1;
        if((A==NULL && B==NULL) || m<0 || n<0)
            return (double)ret;
        if(A==NULL || m<=0){
            if(n & 1)
                return (double)B[n/2];
            else{
                return (double)(B[n/2]+B[n/2-1]) / 2.0;
            }
        }
        if(B==NULL || n<=0){
            if(m & 1)
                return (double)A[m/2];
            else{
                return (double)(A[m/2]+A[m/2-1]) / 2.0;
            }
        }
        int mid = (m+n)/2;
        int pre = 0;
        int i=0, j=0;
        int count=0;
        int token = 0;
        while(i<m && j<n){
            if(A[i]<=B[j]){
                if(count!=mid)
                    pre = A[i];
                if(count==mid){
                    token = A[i];++count;
                    break;
                }
                ++i;
            }else{
                if(count!=mid)
                    pre = B[j];
                if(count==mid){
                    token = B[j];++count;
                    break;
                }
                ++j;
            }
            ++count;
        }
        if(count<=mid){
            if(i<m){
                if(count==mid){
                    token = A[i];
                }else{
                    while(count<mid){
                        pre = A[i++];
                        ++count;
                    }
                    token = A[i];
                }
            }
            if(j<n){
                if(count==mid){
                    token = B[j];
                }else{
                    while(count<mid){
                        pre = B[j++];
                        ++count;
                    }
                    token = B[j];
                }
            }
        }
        
        if((m+n) & 1 ){
            //odd
            return (double)(token);
        }
        else{
            //even
            return (double)(pre+token)/2.0;
        }
    }
};

參考

http://blog.csdn.net/yutianzuijin/article/details/11499917

-----------------------------------------------------------------------------------------------------------------------------------

如果你覺得本篇對你有收穫,請幫頂。
另外,我開通了微信公衆號--分享技術之美,我會不定期的分享一些我學習的東西.
你可以搜索公衆號:swalge 或者掃描下方二維碼關注我

(轉載文章請註明出處: http://blog.csdn.net/swagle/article/details/29197389 )

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章