Python, LeetCode, 4. 两个排序数组的中位数

  • Post author:
  • Post category:python


class Solution:
    def findMedianSortedArrays(self, nums1, nums2):
        """
        :type nums1: List[int]
        :type nums2: List[int]
        :rtype: float
        """
        nums = []
        while len(nums1) != 0 and len(nums2) != 0:
            if nums1[0] < nums2[0]:
                nums.append(nums1.pop(0))
            else:
                nums.append(nums2.pop(0))
        if len(nums1) != 0:
            nums += nums1
        else:
            nums += nums2
        if len(nums) % 2 == 0:
            tmp = int(len(nums)/2)
            res = (nums[tmp] + nums[tmp-1])/2
        else:
            res = nums[len(nums)//2]
        return float(res)



版权声明:本文为u010342040原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。