class Solution:
def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float:
nums1_len = len(nums1)
nums2_len = len(nums2)
def find(nums1,nums2,k):
# time.sleep(1)
if not nums1:
return nums2[k-1]
if not nums2:
return nums1[k-1]
if k == 1:
if nums1 and nums2:
return min(nums1[0],nums2[0])
if nums1:
return nums1[0]
if nums2:
return nums2[0]
nums1_len = len(nums1)
nums2_len = len(nums2)
index = k//2
if index <= nums1_len and index <= nums2_len:
if nums1[index-1]<nums2[index-1]:
return find(nums1[index:],nums2,k-index)
elif nums1[index-1]>=nums2[index-1]:
return find(nums1,nums2[index:],k-index)
else:
if index>nums1_len:
index1 = nums1_len
index2 = k - nums1_len
else:
index2 = nums2_len
index1 = k - nums2_len
if nums1[index1-1]<nums2[index2-1]:
return find(nums1[index1:],nums2,k-index1)
elif nums1[index1-1]>=nums2[index2-1]:
return find(nums1,nums2[index2:],k-index2)
if (nums1_len+nums2_len)%2 == 0:
left = find(nums1,nums2,(nums1_len+nums2_len)//2)
right = find(nums1,nums2,(nums1_len+nums2_len)//2+1)
return ((left+right)/2)
else:
return (find(nums1,nums2,(nums1_len+nums2_len)//2+1))
标签:index,return,Python,中位数,len,find,升序,nums1,nums2
From: https://www.cnblogs.com/DCFV/p/18399964