欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

leetcode题目训练4

程序员文章站 2022-06-10 14:08:53
...

leetcode题目训练4

class Solution:
    def findMedianSortedArrays(self, nums1, nums2):
        size1 = len(nums1)
        size2 = len(nums2)
        if size1 > size2:
            return Solution.findMedianSortedArrays(self, nums2, nums1)
        l = 0
        r = 2 * size1
        while l <= r:
            c1 = int((l + r) // 2)
            c2 = size1 + size2 - c1
            if c1 == 0:
                l1 = -1000000
            else:
                l1 = nums1[int((c1-1)//2)]
            if c1 == 2 * size1:
                r1 = 1000000
            else:
                r1 = nums1[int(c1//2)]
            if c2 == 0:
                l2 = -1000000
            else:
                l2 = nums2[int((c2-1)//2)]
            if c2 == 2 * size2:
                r2 = 1000000
            else:
                r2 = nums2[int(c2//2)]
            if(l1 > r2):
                r = c1 - 1
            elif(l2 > r1):
                l = c1 + 1
            else:
                break
        return (max(l1, l2) + min(r1, r2)) / 2.0
leetcode题目训练4