标签:
public class Solution { public double findMedianSortedArrays(int[] nums1, int[] nums2) { int k = nums1.length + nums2.length; if (k % 2 == 0) { return (findKth(nums1, nums2, 0, 0, k /2) + findKth(nums1, nums2, 0, 0, k/2+1))/2.0; } else { return findKth(nums1, nums2, 0, 0, k/2+1); } } //find Kth public static int findKth(int[] A, int[] B, int A_start, int B_start, int k) { if (A_start >= A.length) { return B[B_start + k - 1]; } if (B_start >= B.length) { return A[A_start + k - 1]; } if (k == 1) { return Math.min(A[A_start], B[B_start]); } int A_k = A_start + k/2 - 1 < A.length ? A[A_start + k/2 - 1] : Integer.MAX_VALUE; int B_k = B_start + k/2 - 1 < B.length ? B[B_start + k/2 - 1] : Integer.MAX_VALUE; if (A_k < B_k) { return findKth(A, B, A_start + k/2, B_start, k - k/2); } else { return findKth(A, B, A_start, B_start + k/2, k - k/2); } } }
标签:
原文地址:http://www.cnblogs.com/77rousongpai/p/4484686.html