leetcode 两正序数组的中位数

class Solution:
    def findMedianSortedArrays(self, nums1, nums2) :
        le1 = len(nums1)
        le2 = len(nums2)
        mid = int((le1 + le2) / 2)
        i = 0
        j = 0
        flag = 0
        pre = 0
        las = 0
        cur = -1
        while i < le1 and j < le2 and cur != mid:
            if flag:
                las = pre
            else:
                flag = 1
            if nums1[i] < nums2[j]:
                pre = nums1[i]
                i += 1
            else:
                pre = nums2[j]
                j += 1
            cur += 1
        while cur != mid and i < le1:
            las = pre
            pre = nums1[i]
            i += 1
            cur += 1
        while cur != mid and j < le2:
            las = pre
            pre = nums2[j]
            j += 1
            cur += 1

        if  (le1 + le2) % 2:
            return float(pre)
        else:
            return (pre + las) / 2





if __name__ == '__main__':
    so = Solution()
    print(so.findMedianSortedArrays([1,2],[3]))

你可能感兴趣的:(leetcode)