classSolution:deffindMedianSortedArrays(self,nums1: List[int],nums2: List[int]) ->float: n, m =len(nums1),len(nums2) index_mid, b_min = (n + m) //2, (n + m) %2 left = right =-1 first = second =0for _ inrange(index_mid +1): left = rightif first == n: right = nums2[second] second +=1elif second == m: right = nums1[first] first +=1elif nums1[first]<= nums2[second]: right = nums1[first] first +=1else: right = nums2[second] second +=1return right if b_min else (left + right) /2.0
classSolution:deffindMedianSortedArrays(self,nums1: List[int],nums2: List[int]) ->float: n, m =len(nums1),len(nums2) total_length = n + mdefget_k_th(k): first = second =0# 指针while1:if first == n:return nums2[second + k -1]elif second == m:return nums1[first + k -1]elif k ==1:returnmin(nums1[first], nums2[second]) rad = k //2-1 next_first =min(n -1, first + rad) next_second =min(m -1, second + rad)if nums1[next_first]<= nums2[next_second]: k -= (next_first - first +1) first = next_first +1else: k -= (next_second - second +1) second = next_second +1if (n + m) %2:returnget_k_th(total_length //2+1)else:return (get_k_th(total_length //2)+get_k_th(total_length //2+1)) /2.0