-
Notifications
You must be signed in to change notification settings - Fork 0
/
L4.java
38 lines (34 loc) · 1.22 KB
/
L4.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package Algorithm;
public class L4 {
public static double findMedianSortedArrays(int A[], int B[]) {
int m = A.length;
int n = B.length;
int total = m + n;
if(total % 2 != 0) {
return (double) findKth(A, 0, m - 1, B, 0, n - 1, total / 2 + 1);
}else {
double x = findKth(A, 0, m - 1, B, 0, n - 1, total / 2);
double y = findKth(A, 0, m - 1, B, 0, n - 1, total / 2 + 1);
return (x + y) / 2;
}
}
public static int findKth(int[] A, int astart, int aend, int[] B, int bstart, int bend, int k) {
int m = aend - astart + 1;
int n = bend - bstart + 1;
if(m > n)
return findKth(B, bstart, bend, A, astart, aend, k);
if(m == 0)
return B[k - 1]; //这里的意思是A中所有元素都要比B[k - 1]要小
if(k == 1)
return Math.min(A[astart], B[bstart]); //代表这如果total为偶数,那么k=1时返回小的那个数字
int partA = Math.min(k / 2, m);
int partB = k - partA;
if(A[astart + partA - 1] < B[bstart + partB - 1]) {
return findKth(A, astart + partA, aend, B, bstart, bend, k - partA);
}else if (A[astart + partA - 1] > B[bstart + partB - 1]) {
return findKth(A, astart, aend, B, bstart + partB, bend, k - partB);
}else {
return A[astart + partA - 1];
}
}
}