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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
|
public class A4MedianOfTwoSortedArray { public static double findMedianSortedArrays(int[] nums1, int[] nums2) { int la = nums1.length; int lb = nums2.length; if (la == lb && lb == 0) { return 0; } if ((la + lb) % 2 == 0) { int m1 = (la + lb) / 2; int m2 = m1 + 1; return (findM(nums1, nums2, m1) + findM(nums1, nums2, m2)) * 1.0 / 2.0; } else { return findM(nums1, nums2, (la + lb + 1) / 2); } }
private static int findM(int[] a, int[] b, int m) { int la = a.length; int lb = b.length; int counter = 0; int t1 = 0, t2 = 0; int result; while (true) { if (t1 == la) { result = b[t2]; t2++; } else if (t2 == lb) { result = a[t1]; t1++; } else if (a[t1] <= b[t2]) { result = a[t1]; t1++; } else { result = b[t2]; t2++; } counter++; if (counter == m) break; } return result; }
public static void main(String[] args) { System.out.println(findMedianSortedArrays(new int[]{1, 3, 5, 6}, new int[]{2, 7, 9, 10})); }
|