给定两个递增数组arr1和arr2,已知两个数组的长度都为N,求两个数组中所有数的上中位数。
上中位数:假设递增序列长度为n,为第n/2个数
数据范围:
,
要求:时间复杂度
,空间复杂度
进阶:时间复杂度为
,空间复杂度为
[1,2,3,4],[3,4,5,6]
3
总共有8个数,上中位数是第4小的数,所以返回3。
[0,1,2],[3,4,5]
2
总共有6个数,那么上中位数是第3小的数,所以返回2
[1],[2]
1
class Solution: def findMedianinTwoSortedAray(self , arr1: List[int], arr2: List[int]) -> int: n = len(arr1) k = 0 index1,index2 = 0,0 for i in range(0,n): if arr1[index1] > arr2[index2]: k = arr2[index2] index2 += 1 else: k = arr1[index1] index1 += 1 return k