Skip to content

Latest commit

 

History

History
executable file
·
69 lines (55 loc) · 1.61 KB

004--Median of Two Sorted Arrays.md

File metadata and controls

executable file
·
69 lines (55 loc) · 1.61 KB
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
# 在没有时间复杂度限制的情况下可以随便撸
class Solution(object):
    def findMedianSortedArrays(self, nums1, nums2):
        """
        :type nums1: List[int]
        :type nums2: List[int]
        :rtype: float
        """
        
        nums1.extend(nums2)
        nums1.sort()
        if len(nums1)%2 == 0:
            half0 = len(nums1) // 2  - 1 
            half1 = len(nums1) // 2  
            return (nums1[half0] + nums1[half1]) / 2.
        if len(nums1)%2 == 1:
            half = len(nums1) // 2  
            return nums1[half]
# 但是是有时间复杂度限制的。 O(log(m+n))
class Solution(object):
    def findMedianSortedArrays(self, nums1, nums2):
        """
        :type nums1: List[int]
        :type nums2: List[int]
        :rtype: float
        """
        
        a, b = sorted((nums1, nums2), key=len)
        m, n = len(a), len(b)
        after = (m + n - 1) / 2
        lo, hi = 0, m
        while lo < hi:
            i = (lo + hi) / 2
            if after-i-1 < 0 or a[i] >= b[after-i-1]:
                hi = i
            else:
                lo = i + 1
        i = lo
        nextfew = sorted(a[i:i+2] + b[after-i:after-i+2])
        return (nextfew[0] + nextfew[1 - (m+n)%2]) / 2.0