注册一亩三分地论坛,查看更多干货!
您需要 登录 才可以下载或查看附件。没有帐号?注册账号 
x
本帖最后由 will0688 于 2013-9-3 10:22 编辑
Find median of two sorted array. 下面是个别人答案。为啥他的速度是log(m+n),我觉得是(m+n)/2啊。
http://discuss.leetcode.com/questions/142/median-of-two-sorted-arrays/2142
public double findMedianSortedArrays(int A[], int B[]) { int a=0,b=0,result=0,odd=0;
boolean IsOdd=false;
if((A.length+B.length)%2==0) IsOdd=true;
for(int i=0;i<(A.length+B.length)/2+1;i++) {
if(b==B.length) result=A[a++];
else if(a==A.length) result=B[b++];
else {
if((A[a]>B[b])) result=B[b++];
else result=A[a++];
}
if(IsOdd&&i+1==(A.length+B.length)/2) odd=result;
}
return IsOdd?((double)result+(double)odd)/2:(double)result;
}
|