class Solution {
public double findMedianSortedArrays(int[] nums1, int[] nums2) {
int n1 = nums1.length;
int n2 = nums2.length;
int s = n1 + n2;
int k = s / 2;
if(s % 2 == 0) {
int x1 = find(nums1, 0, nums2, 0, k);
int x2 = find(nums1, 0, nums2, 0, k + 1);
return (x1 + x2) / 2.0;
}
else {
return find(nums1, 0, nums2, 0, k + 1);
}
}
public int find(int[] nums1, int s1, int[] nums2, int s2, int k) {
if(nums1.length - s1 > nums2.length - s2) return find(nums2, s2, nums1, s1, k);
if(nums1.length == s1) return nums2[s2 + k - 1];
if(k == 1) return Math.min(nums1[s1], nums2[s2]);
int ss1 = Math.min(s1 + k / 2, nums1.length);
int ss2 = s2 + k / 2;
if(nums1[ss1 - 1] < nums2[ss2 - 1]) {
return find(nums1, ss1, nums2, s2, k - (ss1 - s1));
}
else {
return find(nums1, s1, nums2, ss2, k - (ss2 - s2));
}
}
}