问题
![在这里插入图片描述](https://img-blog.csdnimg.cn/96439827ea624d3da8bd452d311c3ba7.png)
代码注释
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
int getKthElement(const vector<int> &nums1, const vector<int> &nums2, int k) {
int m = nums1.size();
int n = nums2.size();
int index1 = 0, index2 = 0;
while (true) {
if (index1 == m) {
return nums2[index2 + k -1];
}
if (index2 == n) {
return nums1[index1 + k - 1];
}
if (k == 1) {
return min(nums1[index1], nums2[index2]);
}
int newIndex1 = min(index1 + k / 2 - 1, m - 1);
int newIndex2 = min(index2 + k / 2 - 1, n - 1);
int pivot1 = nums1[newIndex1];
int pivot2 = nums2[newIndex2];
if (pivot1 <= pivot2) {
k -= newIndex1 - index1 + 1;
index1 = newIndex1 + 1;
}
else {
k -= newIndex2 - index2 + 1;
index2 = newIndex2 + 1;
}
}
}
double findMedianSortedArrays(vector<int> &nums1, vector<int> &nums2) {
int totalLength = nums1.size() + nums2.size();
if (totalLength % 2 == 1) {
return getKthElement(nums1, nums2, (totalLength + 1) / 2);
}
else {
return (getKthElement(nums1, nums2, totalLength / 2) + getKthElement(nums1, nums2, totalLength / 2 + 1)) / 2.0;
}
}
};
int main() {
Solution sol;
vector<int> nums1 = {1, 3, 4, 9};
vector<int> nums2 = {1, 2, 3, 4, 5, 6, 7, 8, 9};
cout << sol.findMedianSortedArrays(nums1, nums2) << endl;
}