[LeetCode]4. Median of Two Sorted Arrays两个排序数组合并后的中位数

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

解法一:不考虑时间复杂度限制在O(log(m+n)),则将两个数组遍历一遍即可以组合成一个排好序的数组,然后取数组的中位数即可,时间复杂度O(m+n):

class Solution {
public:
    double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
        int m = nums1.size();
        int n = nums2.size();
        vector<int> vi(m + n, 0);
        int i = 0;
        int j = 0;
        int k = 0;

        if(m == 0)
        {
            if(n == 0)
                return 0;
            else
                return (n % 2 == 0) ? (nums2[n / 2 - 1] + nums2[n / 2]) / 2.0 : nums2[n / 2];
        }
        if(n == 0)
        {
            if(m == 0)
                return 0;
            else
                return (m % 2 == 0) ? (nums1[m / 2 - 1] + nums1[m / 2]) / 2.0 : nums1[m / 2];
        }

        if(m + n == 1)
            return (m == 1) ? nums1[0] : nums2[0];

        for(int i = 0; i < m;)
        {
            if(j < n && nums1[i] > nums2[j])
            {
                vi[k] = nums2[j];
                j++;
                k++;
            }
            else
            {
                vi[k] = nums1[i];
                i++;
                k++;
            }
        }
        while(j < n)
        {
            vi[k] = nums2[j];
            j++;
            k++;
        }

        if((m + n) % 2 == 0)
            return (vi[(m + n) / 2 - 1] + vi[(m + n) / 2]) / 2.0;
        else
            return vi[(m + n) / 2];
    }
};

解法二:考虑时间复杂度要在O(log(m+n)),则不能简单的从头至尾遍历两个数组。因为数组都是排序的,一种自然地想法即是考虑二分查找。找到两个排序数组的中位数的一个延伸问题是找到两个排序数组的第K大元素,中位数是K=(m+n)/2的情况。可以这样考虑:先分别找到两个排序数组的第K大元素进行比较,即比较arr1[k/2-1]和arr2[k/2-1]的大小,假设arr1[k/2-1]>arr2[k/2-1],则显而易见arr2中排在arr2[k/2-1]之前的元素不可能是合并后排序数组的中位数,因为arr2[k/2-1]不可能大于合并后数组的第K大值,而arr2[0]到arr2[k/2-2]均小于arr2[k/2-1],则更不可能是合并后数组的第K大值了,因此这部分可以丢弃;然后再对一个旧的排序数组和一个新的排序数组递归调用上述过程,最后即可得到合并后排序数组的第K个数。

需要注意的是需要针对数组长度小于K/2的情况单独考虑。

class Solution {
public:
    double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
        int m = nums1.size();
        int n = nums2.size();
        int t = m + n;

        if (m == 0 || n == 0)
        {
            if (m == 0 && n == 0)
                return -1;
            if (m == 0 && n > 1)
            {
                if (n % 2 == 0)
                    return (nums2[n / 2 - 1] + nums2[n / 2]) / 2.0;
                else
                    return nums2[n / 2];
            }
            if (n == 0 && m > 1)
            {
                if (m % 2 == 0)
                    return (nums1[m / 2 - 1] + nums1[m / 2]) / 2.0;
                else
                    return nums1[m / 2];
            }
        }
        if (t == 1)
            return (m == 0) ? nums2[0] : nums1[0];
        if (t == 2)
            return (nums1[0] + nums2[0]) / 2.0;

        if (t % 2 == 0)
            return (findKth(nums1, m, nums2, n, t / 2) + findKth(nums1, m, nums2, n, t / 2 + 1)) / 2.0;
        else
            return findKth(nums1, m, nums2, n, t / 2 + 1);
    }
private:
    double findKth(vector<int> a, int as, vector<int> b, int bs, int k)
    {
        if (as > bs)
            return findKth(b, bs, a, as, k);

        if (k <= 0)
            return -1;
        if (as == 0 || bs == 0)
        {
            if (as == 0 && bs == 0)
                return -1;
            if (as == 0)
                return b[k - 1];
            if (bs == 0)
                return a[k - 1];
        }
        if (k == 1)
            return a[0] > b[0] ? b[0] : a[0];

        int pa = k / 2 > as ? as : k / 2;
        int pb = k - pa;
        if (a[pa - 1] > b[pb - 1])
            return findKth(a, as, vector<int>(b.begin() + pb, b.end()), bs - pb, k - pb);
        else if (a[pa - 1] < b[pb - 1])
            return findKth(vector<int>(a.begin() + pa, a.end()), as - pa, b, bs, k - pa);
        else
            return a[pa - 1];
    }
};
时间: 2024-08-14 15:27:04

[LeetCode]4. Median of Two Sorted Arrays两个排序数组合并后的中位数的相关文章

[LeetCode] 4. Median of Two Sorted Arrays 两个有序数组的中位数

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)). 求两个有序数组的中位数,并限制了时间复杂度O(log (m+n)),看到这个时间复杂度,自然想到用二分搜索Binary Search. 对于一个长度为n的已

[LintCode] 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. Have you met this question in a real interview? Example Given A=[1,2,3,4,5,6] and B=[2,3,4,5], the median is 3.5. Given A=[1,2,3] and B=[4,5],

[LeetCode] Median of Two Sorted Arrays 两个有序数组的中位数

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)). http://www.acmerblog.com/leetcode-median-of-two-sorted-arrays-5330.html http:/

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][JavaScript]Median of Two Sorted Arrays

Median of Two Sorted Arrays 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)). https://leetcode.com/problems/median-of-two-sorted

LeetCode OJ - 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)). 解题思路: 将原问题转变成一个寻找第k小数的问题(假设两个原序列升序排列),这样中位数实际上是第(m+n)/2小的数.所以只要解决了第k小数的问题,原问题也得以解决

【题解】【数组】【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

顺序表 | 二分查找:两个数组合并后的中位数

输入两个长度相同的升序数组,返回这两个数组合并后的中位数 C++代码: int bisearch_midNum(int a[],int b[],int n){ int s1=0,s2=0,d1=n-1,d2=n-1,m1,m2; while(s1!=d1 || s2!=d2){//只要a,b序列同时出现了s==d的情况,才能False退出 m1=(s1+d1)/2; m2=(s2+d2)/2; system("pause"); if(a[m1]==b[m2]) return a[m1]

第三周 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的长度为奇数的情