There is sorted arrays A and B of size m and N respectively. Find The median of the sorted arrays. The overall run time complexity should be O (log (m+n)).
Difficulty: Consider the problem comprehensively, especially the boundary problem
Error: 1. Because Python does not define integers or floating-point numbers in advance, it causes errors
2.
classSolution:#@return A float deffindmediansortedarrays (self, A, B): la,lb=Len (A), Len (B) LC= La +lbifLA*LB = =0:c= AifLB = = 0ElseBiflc%2: returnC[lc/2] Else: return(C[LC/2-1]+C[LC/2])/2Else: I,j=0,0iflc%2: forIndexinchRange (0,LC/2): ifa[i]<B[j]:ifI! = La-1: I=i+1Else: returnb[lc/2-La]Else: ifJ! = Lb-1: J=j+1Else: returna[lc/2-lb]returnA[i]ifA[i] < B[j]ElseB[j]Else: Num=0 forIndexinchRange (0,LC/2): ifa[i]<b[j]: num=A[i]ifI! = La-1: I=i+1Else: ifLc/2-index-1 >0:return(B[lc/2-la-1]+b[lc/2-la])/2Else: return(Num+b[lc/2-la])/2Else: Num=B[j]ifJ! = Lb-1: J=j+1Else: ifLc/2-index-1 >=0:return(a[lc/2-lb-1]+a[lc/2-lb])/2Else: return(num+a[lc/2-lb])/2return(A[i]+num)/2ifA[i] < B[j]Else(B[j]+num)/2
Median of the Sorted Arrays solution