@@ -41,19 +41,30 @@ public double findMedianSortedArrays(int[] A, int[] B) {
4141 }
4242
4343 public double getkth (int [] A , int aStart , int [] B , int bStart , int k ) {
44- if (aStart > A .length - 1 ) return B [bStart + k - 1 ];
45- if (bStart > B .length - 1 ) return A [aStart + k - 1 ];
46- if (k == 1 ) return Math .min (A [aStart ], B [bStart ]);
44+ if (aStart > A .length - 1 ) {
45+ return B [bStart + k - 1 ];
46+ }
47+ if (bStart > B .length - 1 ) {
48+ return A [aStart + k - 1 ];
49+ }
50+ if (k == 1 ) {
51+ return Math .min (A [aStart ], B [bStart ]);
52+ }
4753
4854 int aMid = Integer .MAX_VALUE ;
4955 int bMid = Integer .MAX_VALUE ;
50- if (aStart + k / 2 - 1 < A .length ) aMid = A [aStart + k / 2 - 1 ];
51- if (bStart + k / 2 - 1 < B .length ) bMid = B [bStart + k / 2 - 1 ];
56+ if (aStart + k / 2 - 1 < A .length ) {
57+ aMid = A [aStart + k / 2 - 1 ];
58+ }
59+ if (bStart + k / 2 - 1 < B .length ) {
60+ bMid = B [bStart + k / 2 - 1 ];
61+ }
5262
53- if (aMid < bMid )
63+ if (aMid < bMid ) {
5464 return getkth (A , aStart + k / 2 , B , bStart , k - k / 2 );// Check: aRight + bLeft
55- else
65+ } else {
5666 return getkth (A , aStart , B , bStart + k / 2 , k - k / 2 );// Check: bRight + aLeft
67+ }
5768 }
5869 }
5970
@@ -98,9 +109,15 @@ public double findMedianSortedArrays(int[] A, int[] B, int K) {
98109 }
99110 }
100111
101- if (highA == 0 && highB == 0 ) return 0 ;
102- if (highA == 0 ) return B [highB - 1 - K ];
103- if (highB == 0 ) return A [highA - 1 - K ];
112+ if (highA == 0 && highB == 0 ) {
113+ return 0 ;
114+ }
115+ if (highA == 0 ) {
116+ return B [highB - 1 - K ];
117+ }
118+ if (highB == 0 ) {
119+ return A [highA - 1 - K ];
120+ }
104121 return max (A [highA - 1 ], B [highB - 1 ]);
105122 }
106123 }
0 commit comments