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)).
Solution:
1 Public classSolution {2 Public DoubleFindmediansortedarrays (intA[],intb[]) {3 if(a.length<b.length) findmediansortedarrays (b,a);4 5 intLenA =a.length;6 intLenB =b.length;7 intmin=0,max=LenA;8 intI=-1,j=-1;9 while(min<=max) {Teni = (max+min)/2; Onej = (Lena+lenb)/2-i; A if(j<0) -max = I-1; - Else if(j>LenB) theMin = i+1; - Else if(I<lena && j>0 && a[i]<b[j-1]) -Min = i+1; - Else if(i>0 && J<lenb && a[i-1]>B[j]) +max = I-1; - Else Break; + } A at intNUM1 =Integer.max_value; - if(i>=0 && i<lena) NUM1 =math.min (Num1,a[i]); - if(j>=0 && j<lenb) NUM1 =math.min (Num1,b[j]); - - if((LENA+LENB)%2==1)returnNUM1; - in intnum2 =Integer.min_value; - if(i-1>=0 && i-1<lena) num2 = Math.max (num2,a[i-1]); to if(j-1>=0 && j-1<lenb) num2 = Math.max (num2,b[j-1]); + - Doubleres = (Double) (NUM1+NUM2)/2; the * returnRes; $ Panax Notoginseng - } the}
Leetcode-median of Sorted Arrays