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 ...
随机推荐
- rsyslog 传输mysql 日志
在另外一种环境中,让我们假定你已经在机器上安装了一个名为"foobar"的应用程序,它会在/var/log下生成foobar.log日志文件.现在,你想要将它的日志定向到rsysl ...
- poj2013---二维数组指针使用
#include <stdio.h> #include <stdlib.h> #include<string.h> int main() { ; ][],arr2[ ...
- oracle字符集
oracle server端字符集
- DBCP|C3P0参数详解
1.<!-- 数据源1 --> 2. <bean id="dataSource" 3. class="org.apache.commons.dbcp.B ...
- 如何制作一个类似Tiny Wings的游戏(2) Cocos2d-x 2.1.4
在第二篇<如何制作一个类似Tiny Wings的游戏>基础上,增加添加主角,并且使用Box2D来模拟主角移动,原文<How To Create A Game Like Tiny Wi ...
- WebStorm 7.0 注冊码
经測试 WebStorm 6 7均能够使用例如以下注冊码,简直就是神key啊! WebStorm 6.0 7.0 注冊码 User Name: EMBRACE License Key: ===== ...
- 达内TTS6.0课件oop_day01
- ceph优秀博文
ceph官方博文: http://ceph.com/community/blog/ rgw根据rgw用户来分pool存放数据 http://cephnotes.ksperis.com/blog/201 ...
- winfrom运用webservice上传文件到服务器
winfrom做文件上传的功能显然没有BS的简单,本实例是运用了webservice获取二进制流转换的字符串.然后,解析字符串,把流文件再转成pdf. webservice 里面的代码为下: [Web ...
- 链表的实现 -- 数据结构与算法的javascript描述 第六章
链表 链表是由一组节点组成的集合.每个节点都使用一个对象的引用指向它的后继.指向另一个节点的引用叫做链 结构示意图 : 链表头需要我们标识 head { element:head,next:obj1 ...