二分删除头部法:每次删除两个数组头部中长度为k/2,(k为中位数索引),直到k=1时,返回较小元素即可。



public class Solution {
    /**
     * find median in two sorted array
     * @param arr1 int整型一维数组 the array1
     * @param arr2 int整型一维数组 the array2
     * @return int整型
     */
    public int findMedianinTwoSortedAray (int[] arr1, int[] arr2) {
         int len1 = arr1.length;
         int len2 = arr2.length;
         int sumLen = len1+len2;
         return  deleteNums(arr1,arr2,0,0,sumLen/2);
      }
    public int  deleteNums(int [] nums1,int [] nums2,int index1,int index2,int k){
        if(index1>=nums1.length)return nums2[index2+k-1];
        if(index2>=nums2.length)return nums1[index1+k-1];
        if(k==1)return Math.min(nums1[index1],nums2[index2]);
        int newindex1 = Math.min(index1+k/2,nums1.length)-1;
        int newindex2 = Math.min(index2+k/2,nums2.length)-1;
        if(nums1[newindex1]>nums2[newindex2]){
            return deleteNums(nums1,nums2,index1,newindex2+1,k+index2-newindex2-1);
        }
        else return deleteNums(nums1,nums2,newindex1+1,index2,k+index1-newindex1-1);
    }
}