publicdoublefindMedianSortedArrays(int[] nums1,int[] nums2) {if (nums1 ==null|| nums2 ==null) {return0.0; }int n =nums1.length;int m =nums2.length;int len = n + m;if (len %2!=0) {returnfindKth(nums1,0, nums2,0, len /2+1)*1.0; } else {return (findKth(nums1,0, nums2,0, len /2)+findKth(nums1,0, nums2,0, len /2+1)) /2.0; }}privateintfindKth(int[] nums1,int s1,int[] nums2,int s2,int k) {if (s1 >=nums1.length) {return nums2[s2 + k -1]; } elseif (s2 >=nums2.length) {return nums1[s1 + k -1]; }if (k ==1) { // base casereturnMath.min(nums1[s1], nums2[s2]); }int mid1 = s1 + k /2-1<nums1.length? nums1[s1 + k /2-1] :Integer.MAX_VALUE;int mid2 = s2 + k /2-1<nums2.length? nums2[s2 + k /2-1] :Integer.MAX_VALUE;if (mid1 < mid2) {returnfindKth(nums1, s1 + k /2, nums2, s2, k - k /2); } else {returnfindKth(nums1, s1, nums2, s2 + k /2, k - k /2); }}
publicdoublefindMedianSortedArrays(int[] A,int[] B) {if (A ==null|| B ==null) {returnDouble.MAX_VALUE; }double res =0.0;ArrayList<Integer> list =newArrayList<>();for (int i =0; i <A.length; i++) {list.add(A[i]); }for (int i =0; i <B.length; i++) {list.add(B[i]); }Collections.sort(list);int len =list.size();if (len %2==0) { res =list.get(len /2) +list.get(len /2-1); res = res /2; } else { res =list.get(len /2); }return res;}