double inline mynext(int A[], int m, int a, int B[], int n, int b){
if (a >= m) return B[b];
if (b >= n) return A[a];
return A[a] < B[b] ? A[a] : B[b];
}
class Solution {
public:
double findMedianSortedArrays(int A[], int m, int B[], int n) {
int count = 0;
int hand = 0;
int next = 0;
int xxx = (m + n + 1) / 2;
int a = 0;
int b = 0;
while (a<m && b<n && count<xxx)
{
if (A[a]<B[b]) { hand = A[a]; a++; }
else { hand = B[b]; b++; }
count++;
}
if (count >= xxx)
{
if ((m + n) % 2 == 0) return (hand + mynext(A,m,a,B,n,b)) / 2.0;
else return hand;
}
else if (a<m){
while (count<xxx) { hand = A[a++]; count++; }
}
else if (b<n){
while (count<xxx) { hand = B[b++]; count++; }
}
if ((m + n) % 2 == 0) return (hand +mynext(A,m,a,B,n,b)) / 2.0;
else return hand;
}
};
leetcode Median of Two Sorted Arrays
最新推荐文章于 2021-03-09 19:39:34 发布