import math
from typing import List
class Solution:
def findMedianSortedArrays(self, nums1: List[int],
nums2: List[int]) -> float:
nums1Size = len(nums1)
nums2Size = len(nums2)
na = nums1Size + nums2Size
ns = []
i = 0
j = 0
m = int(math.floor(na / 2 + 1))
while len(ns) < m:
n = None
if i < nums1Size and j < nums2Size:
if nums1[i] < nums2[j]:
n = nums1[i]
i += 1
else:
n = nums2[j]
j += 1
elif i < nums1Size:
n = nums1[i]
i += 1
elif j < nums2Size:
n = nums2[j]
j += 1
ns.append(n)
d = len(ns)
if na % 2 == 1:
return ns[d - 1]
else:
return _____________________
# %%
s = Solution()
print(s.findMedianSortedArrays([1, 3], [2]))
第二种
import math
from typing import List
class Solution:
def findMedianSortedArrays(self, nums1: List[int],
nums2: List[int]) -> float:
nums1Size = len(nums1)
nums2Size = len(nums2)
na = nums1Size + nums2Size
ns = []
i = 0
j = 0
m = int(math.floor(na / 2 + 1))
while len(ns) < m:
n = None
if i < nums1Size and j < nums2Size:
if nums1[i] < nums2[j]:
n = nums1[i]
i += 1
else:
n = nums2[j]
j += 1
elif i < nums1Size:
n = nums1[i]
i += 1
elif j < nums2Size:
n = nums2[j]
j += 1
ns.append(n)
d = len(ns)
if na % 2 == 1:
return ns[d - 1]
else:
return (ns[d + 1] + ns[d + 2]) / 2.0
'''
(ns[d - 1] - ns[d - 2]) / 2.0
(ns[d - 1] + ns[d - 2]) / 2.0
(ns[d + 1] - ns[d + 2]) / 2.0
'''
# %%
s = Solution()
print(s.findMedianSortedArrays([1,2], [2,3]))