Median of Two Sorted Arrays

tag

array binary-search divide-and-conquer

description

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.

找到两个排好序的数组的中位数

思路

首先想到的是将两个数组合并,在学习归并排序的时候应该写过,不过这个算法的复杂度达不到要求

class Solution
{
public:
        double findMedianSortedArrays(vector& nums1, vector& nums2)
        {
                int m = nums1.size(), n = nums2.size();
                int total = m + n;
                int i = 0, j = 0;
                vector mergeNums(total);
                  
                while(i < m && j < n)
                {
                        if(nums[i] < nums[j])
                        {
                              mergeNums[i + j] = nums[i];
                              ++i;
                        }
                        else
                        {
                              mergeNums[i + j] = nums[j];
                              ++j;
                        }
                }
                while(i < m)
                {
                        mergeNums[i + j] = nums[i];
                        ++i;
                }
                while(j < n)
                {
                        mergeNums[i + j] = nums[j];
                        ++j;                        
                }
                if (total & 0x1)
                      return mergeNums[total / 2];
                else
                      return (mergeNums[total / 2] + mergeNums[total / 2 - 1]) / 2.0;
        }
};

后来还是搜索解决的问题,首先考虑寻找由小变大的第K个数。比较 nums1 的第 K/2 个数和 nums2 的第 K/2 个数

1 . nums1[k/2] < nums2[k/2] 线果一定不在nums1[0..k/2]舍掉
2 . nums2[k/2] < nums1[k/2] 结果一定不在nums2[0...k/2]舍掉
3 . nums1[k/2] == nums1[k/2] 找到了

class Solution
{
public:
    double findKth(vector& nums1, int beg1, int n1, vector& nums2, int beg2, int n2, int k)
    {
        if (n1 > n2) return findKth(nums2, beg2, n2, nums1, beg1, n1, k);
        if (n1 == 0) return nums2[beg2 + k - 1];
        if (k == 1) return nums1[beg1] < nums2[beg2] ? nums1[beg1] : nums2[beg2];
        int k_div_2 = n1 < k / 2 ? n1 : k / 2;
        if (nums1[beg1 + k_div_2 - 1] < nums2[beg2 + k - k_div_2 - 1])
        {
            return findKth(nums1, beg1 + k_div_2, n1 - k_div_2, nums2, beg2, n2, k - k_div_2);
        }
        if (nums1[beg1 + k_div_2 - 1] > nums2[beg2 + k - k_div_2 - 1])
        {
            return findKth(nums1, beg1, n1, nums2, beg2 + k - k_div_2, n2 + k_div_2 - k, k_div_2);
        }
        return nums1[beg1 + k_div_2 - 1];
    }
    double findMedianSortedArrays(vector& nums1, vector& nums2)
    {
        int m = nums1.size(), n = nums2.size();
        int total = m + n;
        if (total & 0x1)
            return findKth(nums1, 0, m, nums2, 0, n, total / 2 + 1);
        else
            return (findKth(nums1, 0, m, nums2, 0, n, total / 2) + findKth(nums1, 0, m, nums2, 0, n, total / 2 + 1)) / 2.0;
    }
};

你可能感兴趣的:(Median of Two Sorted Arrays)