findKthNumber是在当前范围内第k小的数。
class Solution {public: double findMedianSortedArrays(vector & nums1, vector & nums2) { int length1 = nums1.size(); int length2 = nums2.size(); int len = length1 + length2; int mid = len/2; if(len % 2 == 0) return (findKthNumber(nums1,nums2,0,0,mid) + findKthNumber(nums1,nums2,0,0,mid+1))/2.0; else return findKthNumber(nums1,nums2,0,0,mid+1); } int findKthNumber(vector nums1,vector nums2,int start1,int start2,int k){ int length1 = nums1.size(); int length2 = nums2.size(); if(start1 >= length1) return nums2[start2+k-1]; if(start2 >= length2) return nums1[start1+k-1]; if(k == 1) return min(nums1[start1],nums2[start2]); int mid = k/2 - 1; int num1 = start1 + mid >= length1 ? 0x7FFFFFFF : nums1[start1 + mid]; int num2 = start2 + mid >= length2 ? 0x7FFFFFFF : nums2[start2 + mid]; if(num1 > num2) return findKthNumber(nums1,nums2,start1,start2 + mid + 1,k-k/2); else return findKthNumber(nums1,nums2,start1 + mid + 1,start2,k-k/2); } };
https://www.cnblogs.com/lupx/p/lupeixin.html