// to make the time complexity equals to O(log(m+m)) // we need to dichotomy #include<iostream> #include<vector>
usingnamespace std;
classSolution { public: doublefindMedianSortedArrays(vector<int> &nums1, vector<int> &nums2) { int m = nums1.size(); int n = nums2.size(); if ((m + n) % 2 == 1) { returnfindKthSortedArrays(nums1, nums2, (m + n + 1) / 2); } else { int left = findKthSortedArrays(nums1, nums2, (m + n) / 2); int right = findKthSortedArrays(nums1, nums2, (m + n) / 2 + 1); return (left + right) / 2.0; } }
private: // this k starts from 1, not 0, notice here intfindKthSortedArrays(vector<int> &nums1, vector<int> &nums2, int k) { int m = nums1.size(); int n = nums2.size(); int index1 = 0; int index2 = 0;
while (true) { // edge situation // out of the bound if (index1 == m) { return nums2[index2 + k - 1]; } if (index2 == n) { return nums1[index1 + k - 1]; } // k == 1 if (k == 1) { returnmin(nums1[index1], nums2[index2]); }
// normal situation int newIndex1 = min(index1 + k / 2 - 1, m - 1); int newIndex2 = min(index2 + k / 2 - 1, n - 1); if (nums1[newIndex1] < nums2[newIndex2]) { k = k - (newIndex1 - index1 + 1); index1 = newIndex1 + 1; } elseif (nums1[newIndex1] > nums2[newIndex2]) { k = k - (newIndex2 - index2 + 1); index2 = newIndex2 + 1; } else { k = k - (newIndex1 - index1 + 1); index1 = newIndex1 + 1; } }