Skip to content

Latest commit

 

History

History
20 lines (19 loc) · 721 Bytes

4. 寻找两个正序数组的中位数.md

File metadata and controls

20 lines (19 loc) · 721 Bytes
class Solution:
    def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float:
        n = (len(nums1)+len(nums2)+1)//2
        m = (len(nums1)+len(nums2)+2)//2
        return (self.helper(nums1, nums2, n)+self.helper(nums1, nums2, m))/2

    def helper(self, nums1, nums2, k):
        if len(nums1) < len(nums2):
            nums1, nums2 = nums2, nums1
        if len(nums2) == 0:
            return nums1[k-1]
        if k == 1:
            return min(nums1[0], nums2[0])
        temp = min(k//2, len(nums2))
        if nums1[temp-1] < nums2[temp-1]:
            return self.helper(nums1[temp:], nums2, k-temp)
        else:
            return self.helper(nums1, nums2[temp:], k-temp)