代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
find median in two sorted array
@param arr1 int整型一维数组 the array1
@param arr2 int整型一维数组 the array2
@return int整型
#顺便复习了一下合并两个有序数组,原理很简单,倒着用双指针遍历把他们对比大小以后放进新的列表中。写的有点儿长,其他人的只遍历len(arr1)次我觉得比我这个好,一开始没想到,光想着合并了
def findMedianinTwoSortedAray(self , arr1, arr2):
li = [0]*(len(arr1)+len(arr2))
m = len(arr1)-1
n = len(arr2)-1
dummy = len(arr1)+len(arr2)-1
while m >= 0 and n >= 0:
if arr1[m] >= arr2[n]:
li[dummy] = arr1[m]
m -= 1
dummy -= 1
else:
li[dummy] = arr2[n]
n -= 1
dummy -= 1
while m >= 0:
li[dummy] = arr1[m]
m -= 1
dummy -= 1
while n >= 0:
li[dummy] = arr2[n]
n -= 1
dummy -= 1
return li[len(li)//2-1]
# write code here