LeetCode 4. 寻找两个正序数组的中位数
原题链接
困难
作者:
__43
,
2020-09-22 19:54:20
,
所有人可见
,
阅读 405
class Solution {
public:
double findMedianSortedArrays(vector<int>& n1, vector<int>& n2)
{
if (n1.size() > n2.size())
swap(n1, n2);
int n = n1.size(), m = n2.size();
int c1, c2, NRmin, NLmax, MLmax, MRmin, l = 0, r = 2 * n;
while (1)
{
c1 = l + r >> 1;
c2 = m + n - c1;
NLmax = 0 == c1 ? INT_MIN : n1[(c1 - 1) >> 1];
NRmin = 2 * n == c1 ? INT_MAX : n1[c1 >> 1];
MLmax = 0 == c2 ? INT_MIN : n2[(c2 - 1) >> 1];
MRmin = 2 * m == c2 ? INT_MAX : n2[c2 >> 1];
if (NLmax > MRmin) r = c1 - 1;
else if (MLmax > NRmin) l = c1 + 1;
else break;
}
return (max(NLmax, MLmax) + min(NRmin, MRmin)) * 0.5;
}
};