1.移动指针
时间复杂度为O(m+n); //官方给出的二分查找方法的时间复杂度为O(log(m+n))
public double findMedianSortedArrays(int[] nums1, int[] nums2) { int m = nums1.length; int n = nums2.length; int len = m+n; int left = -1, right = -1; int index_nums1 = 0, index_nums2 = 0; //len为奇数或偶数都需要遍历 len/2+1次 for (int i = 0; i < (len/2 + 1); i++) { left = right; //left记录上次right的结果 if (index_nums1 < m && (index_nums2>=n || nums1[index_nums1] < nums2[index_nums2])) { right = nums1[index_nums1++]; }else { right = nums2[index_nums2++]; } } if (len %2 ==0) return (left+right) /2.0; else return right; }