leetcode 004 Median of Two Sorted Arrays 寻找两个有序数组的中位数 python O(logn)

所有Leetcode题目不定期汇总在 Github, 欢迎大家批评指正,讨论交流。
'''
# 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:
    def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float:
        # 如果分割后左右的数字相等,那么分割点就是我们要找的。
        # 两个数组的总长度一定,确定一个数组的分割点另一个必然随之确定。
        # 那么找第一个分割点在短的数组找效率会更高,二分查找。

        m,n = len(nums1),len(nums2)
        if m > n:
            nums1, nums2, m, n = nums2, nums1, n ,m
        if m == 0:
            return (nums2[n//2] + nums2[(n-1)//2] )/2                # 获取排序数字中位数,考虑奇偶性
        l , r = 0 , m
        k = (m + n + 1)//2
        while l < r:
            c1 = l + (r-l)//2
            c2 = k-c1
            if nums1[c1] < nums2[c2-1]:
                l = c1 +1
            else:
                r = c1
        c1 = l
        c2 = k - c1
        res1 = max(nums1[c1-1] if c1 > 0 else float("-inf"), nums2[c2-1] if c2 > 0 else float("-inf") )
        if (m + n) % 2 == 1:
            return res1
        res2 = min(nums1[c1] if c1 < m else float("inf"), nums2[c2] if c2 <n else float("inf"))
        return (res1 + res2) / 2
所有Leetcode题目不定期汇总在 Github, 欢迎大家批评指正,讨论交流。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值