【经典】Median of Two Sorted Arrays

题目:leetcode

Median of Two Sorted Arrays

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

注意:当m+n为偶数时,此题的中位数是中间两个数的平均值。

分析:

1、令数组A始终为长度小的那个数。

2、考虑A为空,B不为空的情况。

3、m+n为偶数和奇数的情况要分开讨论。

4、此题有两种思路:一种是求两个有序数组的第k个数,这里k=(m+n)/2.
另一种思路是每次递归都在中位数的前后删除deletenum长度的数字(这个deletenum约等于数组A的长度的一半
),直到数组A长度为2(这是递归的终止条件)。这里我使用第二种思路,特别要注意的是m==1
和 m==2的情况要单独讨论。第二种情况的时间复杂度为O(log(min(m,n))).

class Solution {
public:
    double findMedianSortedArrays(int A[], int m, int B[], int n) {
         if(m>n)
        return findMedianSortedArrays(B,n,A,m);

        if(m==0)
        {
            if(n==0)
            throw exception();
            if(n & 1)
            {
                return B[(n-1)/2];
            }
            else
            {
                double m1=B[(n-1)/2],m2=B[(n-1)/2+1];
                return (m1+m2)/2;
            }

        }

        int total=m+n;
        if(total & 1)
            return findMedianSortedArrays_up(A,0,m-1,B,0,n-1);
        else
            return ( findMedianSortedArrays_down(A,0,m-1,B,0,n-1)+findMedianSortedArrays_up(A,0,m-1,B,0,n-1) )/2.0;
    }

    //比较小的那个数作为中位数
    double findMedianSortedArrays_up(int A[], int al,int ar, int B[], int bl,int br)
    {
        int lena=ar-al+1,lenb=br-bl+1;
        int mida=al+(ar-al)/2,midb=bl+(br-bl)/2;
        if(lena==1 || lena==2 )
        {
            if(lena==1)
            return one_multiple_up(B,bl,br,A[al]);
            else
            {
                return two_multiple_up(A,al,ar,B,bl,br);
            }

        }
        else
        {
            int deletenum=lena&1?lena/2:lena/2-1;
            if( A[mida]==B[midb])
            {
                return A[mida];
            }
            else if( A[mida]<B[midb])
            {
                return findMedianSortedArrays_up(A,mida,ar,B,bl,br-deletenum);
            }
            else
            {
                int offset=lena&1?0:1;
                return findMedianSortedArrays_up(A,al,mida+offset,B,bl+deletenum,br);
            }
        }
    }

    double one_multiple_up(int num[], int l,int r,int number)
    {
        int mid=l+(r-l)/2,len=r-l+1;
        if(num[mid]==number)
        {
            return num[mid];
        }
        else if(num[mid]<number)
        {
            if(len & 1)
           {
               return num[mid];
            }
          else
            {

               return min(number,num[mid+1]);

            }
        }
        else
        {
            if(len & 1)
            {
                 if(len==1)
                 {
                     return number;
                 }
                 else
                 {
                     return max(number,num[mid-1]);
                 }
            }
            else
            {
                return num[mid];
            }
        }
    }

    double two_multiple_up(int A[], int al,int ar, int B[], int bl,int br)
    {
        int mida=al+(ar-al)/2,midb=bl+(br-bl)/2;
        int lenb=br-bl+1;
        if((A[al]==A[ar] && A[al]==B[midb]) || (A[al]<B[midb] && A[ar]>B[midb]))
        {
            return B[midb];
        }
        else if(A[al]==B[midb] || A[ar]==B[midb])
        {
            return B[midb];
        }
        else if(A[ar]<B[midb])
        {
            if(lenb &1)
            {
                return max(A[ar],B[midb-1]);///////
            }
            else
            {
                if(lenb==2)
                    return A[ar];
                else
                {
                    return max(B[midb-1],A[ar]);
                }
            }
        }
        else//A[al]>B[midb]
        {

            return min(B[midb+1],A[al]);

        }
    }

    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    //比较大的那个数作为中位数
    double findMedianSortedArrays_down(int A[], int al,int ar, int B[], int bl,int br)
    {
        int lena=ar-al+1,lenb=br-bl+1;
        int mida=al+(ar-al)/2,midb=bl+(br-bl)/2;
        if((lena & 1) == 0)
        {
            mida++;
        }
        if((lenb & 1) == 0)
        {
            midb++;
        }
        if(lena==1 || lena==2)
        {
            if(lena==1)
            return one_multiple_down(B,bl,br,A[al]);
            else
            {
                return two_multiple_down(A,al,ar,B,bl,br);
            }

        }
        else
        {
            int deletenum=lena&1?lena/2:lena/2-1;
            if( A[mida]==B[midb])
            {
                return A[mida];
            }
            else if( A[mida]<B[midb])
            {
                int offset=lena&1?0:1;
                return findMedianSortedArrays_down(A,mida-offset,ar,B,bl,br-deletenum);
            }
            else
            {
                //int offset=lena&1?0:1;
                return findMedianSortedArrays_down(A,al,mida,B,bl+deletenum,br);
            }
        }
    }

    double one_multiple_down(int num[], int l,int r,int number)
    {
        int mid=l+(r-l)/2,len=r-l+1;
        if((len & 1) == 0)
        {
            mid++;
        }
        if(num[mid]==number)
        {
            return num[mid];
        }
        else if(num[mid]<number)
        {
            if(len & 1)
           {
               if(len==1)
               {
                   return number;
               }
               else
               {
                   return min(number,num[mid+1]);
               }
            }
          else
            {

               return num[mid];
            }
        }
        else
        {
            if(len & 1)
            {
                 return num[mid];
            }
            else
            {
                return min(number,num[mid-1]);
            }
        }
    }

    double two_multiple_down(int A[], int al,int ar, int B[], int bl,int br)
    {
        int lena=ar-al+1,lenb=br-bl+1;
        int mida=al+(ar-al)/2,midb=bl+(br-bl)/2;
        if((lena & 1) == 0)
        {
            mida++;
        }
        if((lenb & 1) == 0)
        {
            midb++;
        }
        if((A[al]==A[ar] && A[al]==B[midb]) || (A[al]<B[midb] && A[ar]>B[midb]))
        {
            return B[midb];
        }
        else if(A[ar]<=B[midb])
        {

            return max(A[ar],B[midb-1]);
        }
        else if(A[al]>=B[midb])
        {
            if(lenb &1)
            {
                return min(B[midb+1],A[al]);
            }
            else
            {
                if(lenb==2)
                {
                    return A[al];
                }
                else
                {
                    return min(B[midb+1],A[al]);
                }
            }
        }
    }
};
时间: 2024-11-05 22:04:25

【经典】Median of Two Sorted Arrays的相关文章

leetcode | Median of Two Sorted Arrays 寻找2个有序数组中第k大的值

问题 Median of Two Sorted Arrays 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)). 分析 本题更经典通用的描述方式时: 给定2个有序数组,找出2个数组中所有元素中第k大的元素. 思路1 直观思

LeetCode(3) || Median of Two Sorted Arrays

LeetCode(3) || Median of Two Sorted Arrays 题记 之前做了3题,感觉难度一般,没想到突然来了这道比较难的,星期六花了一天的时间才做完,可见以前基础太差了. 题目内容 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

LeetCode——Median of Two Sorted Arrays

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)). 有两个已排序的数组A和B,大小为m 和 n. 找出两数组的中位数 时间复杂度应该为 O(log (m+n)). 简单粗暴的方法就是把两个数组合并成一个数组,排序,取中位数.

第三周 leetcode 4. Median of Two Sorted Arrays (HARD)

4. Median of Two Sorted Arrays 给定两个有序的整数序列.求中位数,要求复杂度为对数级别. 通常的思路,我们二分搜索中位数,对某个序列里的某个数 我们可以在对数时间内通过二分算法求得两个序列中比它小的数,整体复杂度也是对数级别.但是代码实现较为困难. 换一个思路,我们把中位数不要当作一个数,而当作一个序列的划分.划分后,序列的左半部设为L,右半部设为R 满足max(L)<=min(R)且满足len(L)==len(R) 二分搜索这个划分即可.对于A+B的长度为奇数的情

leetcode 2.Median of Two Sorted Arrays

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)). 解题思路:如果是一个已排序数组,求中位数,可以直接计算.因此比较常规的想法是直接将两个有序数组合并

【题解】【数组】【Leetcode】Median of Two Sorted Arrays

Median of Two Sorted Arrays 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)). ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22

周刷题第二期总结(Longest Substring Without Repeating Characters and Median of Two Sorted Arrays)

这周前面刷题倒是蛮开心,后面出了很多别的事情和问题就去忙其他的,结果又只完成了最低目标. Lonest Substring Without Repeating Characters: Given a string, find the length of the longest substring without repeating characters. Examples: Given "abcabcbb", the answer is "abc", which t

LeetCode 第二题,Median of Two Sorted Arrays

题目再现 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)). 题意解析 题目意思是给两个大小为m,n的有序数组(m,n可能为0),要求找出这两个数组的中位数.并且程序的时间复杂度必须不能超过O(log(m+n)). 这道题的

[leetcode]Median of Two Sorted Arrays @ Python

原题地址:https://oj.leetcode.com/problems/median-of-two-sorted-arrays/ 题意: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)). 解题思路:这道题要求两个已经排好