leetcode4 寻找两个有序数组的中位数 python

寻找两个有序数组的中位数(困难)(leetcode4)

给定两个大小为 m 和 n 的有序数组 nums1 和 nums2。请你找出这两个有序数组的中位数,并且要求算法的时间复杂度为 O(log(m + n))。你可假设 nums1 和 nums2 不会同时为空。

示例 1:nums1 = [1, 3],nums2 = [2], 则中位数是 2.0

示例 2:nums1 = [1, 2],nums2 = [3, 4], 则中位数是 (2 + 3)/2 = 2.5

def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float:

        a=sorted(nums1+nums2)

        if len(a)%2==0:

            res=(a[int(len(a)/2-1)]+a[int(len(a)/2)])/2

        else:

            res=a[int((len(a)+1)/2)-1]

        return res

猜你喜欢

转载自blog.csdn.net/qq_37792144/article/details/89173864
今日推荐