[leetcode] Median of Two Sorted Arrays

来源:互联网 发布:数据备份自动还原 编辑:程序博客网 时间:2024/06/05 14:51

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

A1 二分搜索,首先定位在两数组的中点,若左侧数字的最大值大于右侧数字最小值则缩小范围进行二分搜索,同时保证左右侧数字个数差<=1。直至全部左侧数字<=右侧数字搜索完成。 292ms

class Solution {public:    int max(int x, int y)    {    return x>y?x:y;    }        int min(int x, int y)    {    return x<y?x:y;    }        double findSingleMedian(int A[], int m)    {    if (m%2 == 0)    return (A[m/2-1] + A[m/2])/2.0;    else    return A[m/2];    }    double findMedianSortedArrays(int A[], int m, int B[], int n)     {    if (m == 0 && n == 0)    return 0;    if (n == 0)    return findSingleMedian(A,m);    if (m == 0)    return findSingleMedian(B,n);        int *As = new int[m+2];    int *Bs = new int[n+2];        As[0] = Bs[0] = A[0]<B[0]?A[0]:B[0];    memcpy(As+1, A, m*sizeof(int));    memcpy(Bs+1, B, n*sizeof(int));    As[m+1] = Bs[n+1] = A[m-1]>B[n-1]?A[m-1]:B[n-1];        m+=2;    n+=2;        int p = m/2, q = n/2;    if (m%2!=0 && n%2!=0)    q++;        int l_max = As[p-1]>Bs[q-1]?As[p-1]:Bs[q-1];    int r_min = As[p]<Bs[q]?As[p]:Bs[q];        int s_A=0, s_B=0, e_A = m, e_B = n;        while (l_max > r_min)    {    int step;    if (As[p-1]>Bs[q-1])    {      e_A = p;    s_B = q;    step = min(max(1,(e_A-s_A)/2), max(1,(e_B-s_B)/2));    p -= step;    q += step;    }    else    {        s_A = p;    e_B = q;    step = min(max(1,(e_A-s_A)/2), max(1,(e_B-s_B)/2));    p += step;    q -= step;    }    l_max = As[p-1]>Bs[q-1]?As[p-1]:Bs[q-1];    r_min = As[p]<Bs[q]?As[p]:Bs[q];    }        double res;    if ((m+n)%2 != 0)    res = min(As[p], Bs[q]);    else    res = (max(As[p-1], Bs[q-1])+min(As[p], Bs[q]))/2.0;        delete As;    delete Bs;        return res;    }};

A2 实现求第k小数字 244ms

class Solution {public:    int max(int x, int y)    {    return x>y?x:y;    }        int min(int x, int y)    {    return x<y?x:y;    }        double findSingleMedian(int A[], int m)    {    if (m%2 == 0)    return (A[m/2-1] + A[m/2])/2.0;    else    return A[m/2];    }        int findKthNumber(int A[], int m, int B[], int n, int k)    {        if (k == 1)        {            return A[0]<B[0]?A[0]:B[0];        }        if (m+n <= k)        {            return A[m-1]>B[n-1]?A[m-1]:B[n-1];        }        int step;        step = (m<k/2)?m:k/2;        step = (n<step)?n:step;                if (A[step-1]==B[step-1])        {            if (step == m)            {                return B[k-m-1];            }            else if (step == n)            {                return A[k-n-1];            }            else            {                return (k%2==0)?A[k/2-1]:min(A[k/2],B[k/2]);            }        }        else if (A[step-1] < B[step-1])        {            if (step == m)            {                return B[k-m-1];            }            else            {                return findKthNumber(A+step, m-step, B, n, k-step);            }        }        else        {            if (step == n)            {                return A[k-n-1];            }            else            {                return findKthNumber(A, m, B+step, n-step, k-step);            }        }    }    double findMedianSortedArrays(int A[], int m, int B[], int n)     {    if (m == 0 && n == 0)    return 0;    if (n == 0)    return findSingleMedian(A,m);    if (m == 0)    return findSingleMedian(B,n);        if ((m+n)%2 == 0)    {        return (findKthNumber(A,m,B,n,(m+n)/2)+findKthNumber(A,m,B,n,(m+n)/2+1))/2.0;    }    else    {        return findKthNumber(A,m,B,n,(m+n)/2+1);    }        }};



0 0
原创粉丝点击