LeetCode——004-Median-of-Two-Sorted-Arrays

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.

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

solution
when solving the problem saw restriction algorithm complexity O (log (m + 1) ),
you can be excluded Looking after sorting the two median arrays.
We feel topic has prompted the use of dichotomy, but feel unable to use dichotomy here.

While my approach is sorted again determines whether bits, i.e., k used to sort the array A, B,
when k == (A + B) / 2 ] [array size, return directly to the median, the only It is worth noting that the size of the array is even or odd question.

class Solution {
public:
    double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
        int m = nums1.size(), n = nums2.size();
        int i = 0, j = 0, k = 0, mid = (m + n) / 2 + ((m + n) % 2 == 0 ? 0 : 1);
        double sum = 0.0;
        while (i < m && j < n)
        {
            if (nums1[i] <= nums2[j])sum += nums1[i++];
            else sum += nums2[j++];
            ++k;
            if (isReturn(sum, k, mid, m + n))return sum;
        }
        while (i < m)
        {
            sum += nums1[i++];
            ++k;
            if (isReturn(sum, k, mid, m + n))return sum;
        }
        while (j < n)
        {
            sum += nums2[j++];
            ++k;
            if (isReturn(sum, k, mid, m + n))return sum;
        }
        return sum;
    }
    bool isReturn(double &sum, int k, int mid, int s)
    {
        if (k < mid)
        {
            sum = 0.0;
            return false;
        }
        else if (k == mid && s % 2 == 1)
            return true;
        else if (k == mid + 1)
        {
            sum /= 2.0;
            return true;
        }
        return false;
    }
};

Learn
learn about the god of thinking,
using recursive solution, but the general idea as I do.

class Solution {
public:
    double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
        int m = nums1.size(), n = nums2.size(), left = (m + n + 1) / 2, right = (m + n + 2) / 2;
        return (findKth(nums1, 0, nums2, 0, left) + findKth(nums1, 0, nums2, 0, right)) / 2.0;
    }
    int findKth(vector<int>& nums1, int i, vector<int>& nums2, int j, int k) {
        if (i >= nums1.size()) return nums2[j + k - 1];
        if (j >= nums2.size()) return nums1[i + k - 1];
        if (k == 1) return min(nums1[i], nums2[j]);
        int midVal1 = (i + k / 2 - 1 < nums1.size()) ? nums1[i + k / 2 - 1] : INT_MAX;
        int midVal2 = (j + k / 2 - 1 < nums2.size()) ? nums2[j + k / 2 - 1] : INT_MAX;
        if (midVal1 < midVal2) {
            return findKth(nums1, i + k / 2, nums2, j, k - k / 2);
        } else {
            return findKth(nums1, i, nums2, j + k / 2, k - k / 2);
        }
    }
};

Guess you like

Origin www.cnblogs.com/zzw1024/p/12122207.html