(http://leetcode.com/2011/03/median-of-two-sorted-arrays.html)

There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log(m+n)).

double findMedian(int A[], int B[], int l, int r, int nA, int nB)
{
if (l > r)
return findMedian(B, A, max(, (nA+nB)/-nA), min(nB, (nA+nB)/), nB, nA);
int i = (l+r)/;
int j = (nA+nB)/ - i - ;
if (j >= && A[i] < B[j])
return findMedian(A, B, i+, r, nA, nB);
else if (j < nB- && A[i] > B[j+])
return findMedian(A, B, l, i-, nA, nB);
else
{
if ((nA+nB)% == )
return A[i];
else if (i > )
return (A[i]+max(B[j], A[i-]))/2.0;
else
return (A[i]+B[j])/2.0;
}
}

A O(m+n) solution:

bool findMedian(int A[], int B[], int nA, int nB)
{
assert(A && B && nA >= && nB >= ); bool odd = (nA + nB) % == ? true : false;
int medianIndex = (nA+nB)/;
if (odd == false)
medianIndex++; int i = ;
int j = ;
int count = ;
int pre = -;
int cur = -;
while (i < nA && j < nB)
{
count++;
if (A[i] < B[j])
{
if (count == medianIndex)
{
cur = A[i];
break;
}
pre = A[i];
i++;
}
else
{
if (count == medianIndex)
{
cur = B[i];
break;
}
pre = B[j];
j++;
}
}
if (i == nA)
{
cur = B[j+medianIndex-count-];
if (medianIndex-count > )
pre = B[j+medianIndex-count-];
}
else if (j == nB)
{
cur = A[i+medianIndex-count-];
if (medianIndex-count > )
pre = A[i+medianIndex-count-];
} if (odd == true)
return cur;
else
return (cur+pre)/2.0;
}

Median of Sorted Arrays的更多相关文章

  1. No.004:Median of Two Sorted Arrays

    问题: There are two sorted arrays nums1 and nums2 of size m and n respectively.Find the median of the ...

  2. [LeetCode] Median of Two Sorted Arrays 两个有序数组的中位数

    There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...

  3. 【leetcode】Median of Two Sorted Arrays

    题目简述: There are two sorted arrays A and B of size m and n respectively. Find the median of the two s ...

  4. 【leedcode】 Median of Two Sorted Arrays

    https://leetcode.com/problems/median-of-two-sorted-arrays/ There are two sorted arrays nums1 and num ...

  5. leetcode-【hard】4. Median of Two Sorted Arrays

    题目 There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the ...

  6. Leetcode4:Median of Two Sorted Arrays@Python

    There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...

  7. leetcode 4. Median of Two Sorted Arrays

    https://leetcode.com/problems/median-of-two-sorted-arrays/ There are two sorted arrays nums1 and num ...

  8. 【算法之美】求解两个有序数组的中位数 — leetcode 4. Median of Two Sorted Arrays

    一道非常经典的题目,Median of Two Sorted Arrays.(PS:leetcode 我已经做了 190 道,欢迎围观全部题解 https://github.com/hanzichi/ ...

  9. Leetcode Median of Two Sorted Arrays

    There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted ...

随机推荐

  1. 从零开始学 iOS 开发的15条建议

    事情困难是事实,再困难的事还是要每天努力去做是更大的事实. 因为我是一路自学过来的,并且公认没什么天赋的前提下,进步得不算太慢,所以有很多打算从零开始的朋友会问我,该怎么学iOS开发.跟粉丝群的朋友交 ...

  2. Gdal 1.11.0 添加 Postgresql 9.1 sqlite3 支持

    OS环境Ubuntu12.04 32bit 因为公司一个功能要用到gdal 的ogr2ogr命令转换shp数据,需要能往postgis和sqlite 中插入数据. 用gdal1.11.0的源码默认安装 ...

  3. Crisis of HDU(母函数)

    Crisis of HDU Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

  4. Windows消息大全

    最近在写TabControl的用户控件,需要用到sendMessage,已做备份. 引用:http://bbs.aau.cn/forum.php?mod=viewthread&tid=7776 ...

  5. 用ssh建立机器之间的信任机制

    继续整理以前的博客,第二弹:用ssh建立机器之间的信任机制 #!/bin/bash #需要远程添加信任机制的机器 REMOTE[]=db-XXX-XXX1.db01 REMOTE[]=db-XXX-X ...

  6. javascript how sort() work

    Array.prototype.sort() 方法一般用来对数组中的元素进行排序,既可以对数字排序,也可以对字符串进行排序.如果没有指定比较函数,会根据数组中字符的Unicode编码来进行排序.这对于 ...

  7. SQL 语句优化—— (二) 索引的利用

    索引是与表或视图关联的磁盘上结构,可以加快从表或视图中检索行的速度.索引包含由表或视图中的一列或多列生成的键.与书中的索引一样,数据库中的索引使您可以快速找到表或索引视图中的特定信息.索引包含从表或视 ...

  8. JSONObject put accumulate element 方法区别-------java中

    1.public Object put (Object key, Object value) 将value映射到key下.如果此JSONObject对象之前存在一个value在这个key下,当前的va ...

  9. C - Wooden Sticks

    C - Wooden Sticks Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u S ...

  10. C++学习之this指针

    C++学习之this指针 一个对象的this指针并不是对象本身的一部分,不会影响sizeof(对象)的结果.this作用域是在类内部,当在类的非静态成员函数中访问类的非静态成员的时候,编译器会自动将对 ...