(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. material风格的日期/时间选择:SublimePicker

    介绍: 一个material风格的view,提供了各种关于日期选择的功能,可以选择日期,选择时间,选择重复次数等,可以在不离开选择器的情况下在不同的选择界面间切换.其实这些功能是从5.0的日历中抠出来 ...

  2. 每天学点Linux:一

    软链接和硬链接: 软链接,又称符号链接,它的原理是通过一个文本文件记录真实文件在系统中的位置,然后在文件操作的时候通过该地址查找原文件然后对其操作.类似于Windows里面的快捷方式.软链接可以链接不 ...

  3. css样式实现字体删除线效果

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  4. ubuntu13.04安装SenchaArchitect-2.2无法启动的问题

    系统是ubuntukylin-13.04-desktop版本,不知道别的版本有没有这个问题,未测试.SenchaArchitect采用最新版本2.2.2,我安装的是32位的. 具体无法启动的问题如下: ...

  5. 2014 Web开发趋势

    本文翻译自:http://www.pixelstech.net/article/1401629232-Web-design-trends-for-2014 如今,已然到了Web横行的时代.越来越多的资 ...

  6. 赵雅智_BroadcastReceiver

    BroadcastReceiver  用于接收程序(包含用户开放的程序和系统内建程序)所发出的Broadcast intent 耗电量 开机启动 窃取别人短信 窃取别人电话 开发: 创建须要启动的Br ...

  7. MyBatis使用Generator自动生成代码

    MyBatis中,可以使用Generator自动生成代码,包括DAO层. MODEL层 .MAPPING SQL映射文件. 第一步: 配置好自动生成代码所需的XML配置文件,例如(generator. ...

  8. Java面试题之五

    二十一.super()与this()的区别? (1)用this的情况: 1.在构造方法中,通过this调用另一个构造方法,用法:this(参数列表). 2.在函数参数或函数的局部变量与成员变量同名,即 ...

  9. ASP.NET页面上传文件时提示文件大小超过请求解决方法

    在webconfig中节点 <system.web> </system.web> 下加入以下代码:maxRequestLength为限制上传文件大小,executionTime ...

  10. validate 表单验证

    转自博客园:http://www.cnblogs.com/easyinsc/archive/2009/02/27/1407826.html (1)required:true               ...