LeetCoded第四题——median of two sorted array解法

问题描述
There are two sorted arrays nums1 and nums2 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)).

You may assume nums1 and nums2 cannot be both empty.

Example 1:

nums1 = [1, 3]
nums2 = [2]

The median is 2.0
Example 2:

nums1 = [1, 2]
nums2 = [3, 4]

The median is (2 + 3)/2 = 2.5

解法思路
这个问题分为两种情况,当数字个数为奇数时,中位数就是数列中的一个数,但为偶数时,则为数列中间两数的平均值,所以得分两种情况讨论。运用将两个排序好的数列整合成一个数列的方法,从小到大遍历整个数列,使用一个数字p标记搜索到的数字个数,当到达中位数时,记录下来。

C++代码

class Solution {
public:
    double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
        int m=nums1.size();
        int n=nums2.size();
        double median=0;
        int a=0,b=0;
        int p=0;//存储遍历的数量,判断是否达到中位数的位置
        if((m+n)%2==0)
        {
            int r1=(m+n)/2;
            int r2=r1+1;
            while(a<m||b<n)
            {
                if(a>=m)
                {
                    ++p;
                    if(p==r1) median+=nums2[b];
                    else if(p==r2) 
                    {
                        median=(median+nums2[b])/2;
                        break;//得到median的值,跳出while循环
                    }
                    b=b+1;
                   
                }
                else if(b>=n)
                {
                    ++p;
                    if(p==r1) median+=nums1[a];
                    else if(p==r2)
                    {
                        median=(median+nums1[a])/2;
                        break;
                    }
                    a=a+1;
                }
                else
                {
                    if(nums1[a]<nums2[b])//从小到大遍历nums1和nums2中的数字,直到找到中位数
                    {
                        ++p;
                        if(p==r1) median+=nums1[a];
                         else if(p==r2)
                         {
                             median=(median+nums1[a])/2;
                             break;
                          }
                        a=a+1;
                    }
                    else
                    {
                        ++p;
                        if(p==r1) median+=nums2[b];
                          else if(p==r2) 
                         {
                              median=(median+nums2[b])/2;
                             break;//得到median的值,跳出while循环
                         }
                        b=b+1;
                    }
                }
            }
        }
        else//只有一个中位数
        {
            int r=(m+n)/2+1;
            while(a<m||b<n)
            {
                if(a>=m)
                {
                    ++p;
                    if(p==r)
                    {
                        median=nums2[b];
                        break;
                    }
                    b=b+1;
                }
               else if(b>=n)
                {
                   ++p;
                    if(p==r)
                    {
                        median=nums1[a];
                        break;
                    }
                    a=a+1;
                }
                else
                {
                    if(nums1[a]<nums2[b])
                    {
                        ++p;
                        if(p==r)
                        {
                            median=nums1[a];
                            break;
                        }
                        ++a;
                    }
                    else
                    {
                        ++p;
                        if(p==r)
                        {
                            median=nums2[b];
                            break;
                        }
                        ++b;
                    }
                }
            }
        }
        return median;
        
    }
};

结果分析
submit后,leetcode上显示“Runtime: 44 ms, faster than 97.04% of C++ online submissions for Median of Two Sorted Arrays.”初看起来很快,但是经过分析,我的算法的时间复杂度是O((m+n)/2),并不是题目要求的O(log(m+n)),当数字比较多的时候,算法效率达不到题目要求,所以还需要改进。

改进想法

你可能感兴趣的:(LeetCode刷题记录)