您的位置:首页 > 其它

leetcode Median of Two Sorted Arrays

2015-04-30 21:32 211 查看
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