There are two sorted arrays nums1 and nums2 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 findMedianSortedArrays(int* nums1, int nums1Size, int* nums2, int nums2Size) {
int len = nums1Size+nums2Size;
int median = len >> ;
if(len%==) {
if(nums1Size==) return nums2[median];
if(nums2Size==) return nums1[median];
return findK(nums1, , nums1Size, nums2, , nums2Size, median+);
}
else{
if(nums1Size==) return (double)(nums2[median-]+nums2[median])/;
if(nums2Size==) return (double)(nums1[median-]+nums1[median])/;
return (double)(findK(nums1, , nums1Size, nums2, , nums2Size, median)+findK(nums1, , nums1Size, nums2, , nums2Size, median+))/;
}
} int findK(int* nums1, int start1, int len1, int* nums2, int start2, int len2, int k){
if(len1==){ //check len = 1 case, because we keep median when recursion; otherwise, endless loop
if(nums1[start1] < nums2[start2+k-]) return nums2[start2+k-];
else{
if(k > len2 || nums1[start1] < nums2[start2+k-] ) return nums1[start1];
else return nums2[start2+k-];
}
}
if(len2==){
if(nums2[start2] < nums1[start1+k-]) return nums1[start1+k-];
else{
if(k > len1 || nums2[start2] < nums1[start1+k-] ) return nums2[start2];
else return nums1[start1+k-];
}
} int median1 = start1+(len1 >> ); //if len is odd, it's exactly median; else if even, it's the second of the two median
int median2 = start2+(len2 >> ); if(k <= ((len1+len2)>>)){ //k is at the first half
if(nums1[median1] < nums2[median2]){ //delete the second half of nums2
findK(nums1, start1, len1, nums2, start2, median2-start2, k); //1. delete from median (including median)
}
else{//delete the second half of nums1
findK(nums1, start1, median1-start1, nums2, start2, len2, k);
}
}
else{ //k is at the second half
if(nums1[median1] < nums2[median2]){ //delete the first half of nums1
findK(nums1, median1, len1-(median1-start1), nums2, start2, len2, k-(median1-start1)); //2. Each time delete half at most, so keep median
}
else{ //delete the first half of nums2
findK(nums1, start1, len1, nums2, median2, len2-(median2-start2), k-(median2-start2));
}
}
//From 1, 2, we can see, when only one element, it cannot be deleted, so the end loop condition is len = 1
}

4. Median of Two Sorted Arrays (二分法;递归的结束条件)的更多相关文章

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

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

  2. Leetcode 4. Median of Two Sorted Arrays(中位数+二分答案+递归)

    4. Median of Two Sorted Arrays Hard There are two sorted arrays nums1 and nums2 of size m and n resp ...

  3. LeetCode(3) || Median of Two Sorted Arrays

    LeetCode(3) || Median of Two Sorted Arrays 题记 之前做了3题,感觉难度一般,没想到突然来了这道比较难的,星期六花了一天的时间才做完,可见以前基础太差了. 题 ...

  4. 【LeetCode】4. Median of Two Sorted Arrays (2 solutions)

    Median of Two Sorted Arrays There are two sorted arrays A and B of size m and n respectively. Find t ...

  5. 4. Median of Two Sorted Arrays(topK-logk)

    4. Median of Two Sorted Arrays 题目 There are two sorted arrays nums1 and nums2 of size m and n respec ...

  6. LeetCode 第四题 Median of Two Sorted Arrays 二人 渣渣选手乱七八糟分析发现基本回到思路1

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

  7. LeetCode 004 Median of Two Sorted Arrays

    题目描述:Median of Two Sorted Arrays There are two sorted arrays A and B of size m and n respectively. F ...

  8. [LintCode] 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 ...

  9. 2.Median of Two Sorted Arrays (两个排序数组的中位数)

    要求:Median of Two Sorted Arrays (求两个排序数组的中位数) 分析:1. 两个数组含有的数字总数为偶数或奇数两种情况.2. 有数组可能为空. 解决方法: 1.排序法 时间复 ...

随机推荐

  1. C# Uditor 富文本的部署

    <1> 先到http://ueditor.baidu.com/website/download.html去下载对应编程语言的版本(以.net为例) <二> 将下载下来的文件解压 ...

  2. leetcode212

    class Solution { public List<String> findWords(char[][] board, String[] words) { List<Strin ...

  3. MVC登录校验

    利用MVC自带的过滤器可现实简单的登录校验 在项目目录下创建一个BaseController控制器,让需要验证的控制器继承这个BaseController 需要让BaseController继承Con ...

  4. 静态属性@property

    property 作用其实把类里面的逻辑给隐藏起来(封装逻辑,让用户调用的时候感知不到你的逻辑) property实例1:class Room: def __init__(self): pass @p ...

  5. 使用STM32的USART的同步模式Synchronous调戏SPI【usart模拟spi理论】

    [原创出品§转载请注明出处] 出处:http://www.cnblogs.com/libra13179/p/7064321.html 什么东西?? 我们先来看我们平常看到SPI的时序图(呵呵,要是忘记 ...

  6. django 使用mysql 数据库

    在 django 创建项目中 默认使用的是  splite3 数据库,不是mysql 数据库,要使用mysql ,要做一些配置: 在 settings.py 中修改如下: DATABASES = { ...

  7. node 支持es6

    安装  babel-cli, 全局安装 npm install babel-cli -g 然后 在工程目录下 安装 npm install babel-cli --save npm install b ...

  8. VirtualBox配置

    安装增强工具:http://mikemainguy.blogspot.jp/2015/03/installing-virtualbox-guest-additions.html 安装ssh:https ...

  9. XML报错:The reference to entity "characterEncoding" must end with the ';' delimite

    解决方法: 在web.xml增加如下配置: <filter>  <filter-name>encodingFilter</filter-name>  <fil ...

  10. ORACLE数据库 常用命令和Sql常用语句

    ORACLE 账号相关 如何获取表及权限 1.COPY表空间backup scottexp登录管理员账号system2.创建用户 create user han identified(认证) by m ...