Median of Sorted Arrays
(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的更多相关文章
- 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 ...
- [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 ...
- 【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 ...
- 【leedcode】 Median of Two Sorted Arrays
https://leetcode.com/problems/median-of-two-sorted-arrays/ There are two sorted arrays nums1 and num ...
- 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 ...
- 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 ...
- leetcode 4. Median of Two Sorted Arrays
https://leetcode.com/problems/median-of-two-sorted-arrays/ There are two sorted arrays nums1 and num ...
- 【算法之美】求解两个有序数组的中位数 — leetcode 4. Median of Two Sorted Arrays
一道非常经典的题目,Median of Two Sorted Arrays.(PS:leetcode 我已经做了 190 道,欢迎围观全部题解 https://github.com/hanzichi/ ...
- 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 ...
随机推荐
- 运行预构建 Linux 映像的 Windows Azure 虚拟机中的交换空间 – 第 1 部分
本文章由 Azure CAT 团队的 Piyush Ranjan (MSFT) 撰写. 随着基础结构服务(虚拟机和虚拟网络)近期在 Windows Azure 上正式发布,越来越多的企业工作负荷正在向 ...
- 第三届蓝桥杯预赛真题_第一题_两种微生物 X 和 Y
/* 假设有两种微生物 X 和 Y X出生后每隔3分钟分裂一次(数目加倍),Y出生后每隔2分钟分裂一次(数目加倍). 一个新出生的X,半分钟之后吃掉1个Y,并且,从此开始,每隔1分钟吃1个Y. 现在已 ...
- WebService-06-CXF与Spring集成
前言 自3月份到一家快递公司之后,就极少有时间来写博客了,进去的第一个周末就加班.做公司的开放平台,协助一个小伙伴写WebService接口,用的就是CXF.正好这个东西曾经使用过.如今快7月了,曾经 ...
- switch case default 的使用
switch_case从页面输入五个同学的成绩,求出平均成绩,如果大于等于90为优秀,小于90大于等于80为良好,小于80大于等于70为一般,小于70大于等于60为较差,小于60为很差 SWITCH语 ...
- csapp lab2 bomb 二进制炸弹《深入理解计算机系统》
bomb炸弹实验 首先对bomb这个文件进行反汇编,得到一个1000+的汇编程序,看的头大. phase_1: 0000000000400ef0 <phase_1>: 400ef0: 48 ...
- Nancy 搭建
Nancy 框架 1.是一个轻量级用于构建http相应的web框架: 2.与mvc类似,有自己的路由机制: 3.可以处理 DELETE , GET , HEAD , OPTIONS , ...
- Bash shell使用环境的终端的环境设置:stty
Bash shell使用环境的终端的环境设置:stty Bash shell使用环境的终端的环境设置:stty stty -a 将当前所有的stty参数列出来 intr:给正在运行的程序发送中断信号 ...
- php基础知识--文件操作
文件操作 文件 广义角度: 任何一个在磁盘上可以看到的符号(包含真正的文件及文件夹) 狭义角度: 真实存储数据的载体(不包含文件夹, 如doc文件,txt文件等) 文件操作: 对文件的增删改查 文件分 ...
- hdu 1823 Luck and Love 二维线段树
题目链接 很裸的题, 唯一需要注意的就是询问时给出的区间并不是l<r, 需要判断然后交换一下, WA了好多发... #include<bits/stdc++.h> using nam ...
- 阿里云ECS每天一件事D6:安装nginx-1.6.2
自从接触nginx就开始喜欢上这个小东西了,似乎没什么特别的原因,就是喜欢而已. 1.安装环境的准备 yum install pcre pcre-devel openssl openssl-devel ...