python;leetcode 两个排序数组的中位数

贴上本人垃圾解法 大佬解法暂时没看懂 https://leetcode.com/problems/median-of-two-sorted-arrays/discuss/2481/Share-my-O(log(min(mn))-solution-with-explanation

class Solution:
    def findMedianSortedArrays(self, nums1, nums2):
        """
        :type nums1: List[int]
        :type nums2: List[int]
        :rtype: float
        """
        l = list()
        while nums1 and nums2:
            if nums1[0]>nums2[0]:
                l.append(nums2[0])
                nums2.pop(0)
            else:
                l.append(nums1[0])
                nums1.pop(0)
        if len(nums1)==0 :
            l.extend(nums2)
        if len(nums2)==0:
            l.extend(nums1)
        n= len (l)
        if n%2 == 0:
            return((l[int(n/2)]+l[int(n/2-1)])/2)
        else:
            return(l[int(n/2)])

你可能感兴趣的:(python;leetcode 两个排序数组的中位数)