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. Kali 局域网 DNS 劫持

    <一> 所需工具  1: Kali-linux-2017  2: ettercap 0.8.2 3: web 服务器, 这里以 node 为例 <二> 原理  1: DNS劫持 ...

  2. <基础> PHP 进阶之 函数(Function)

    引用参数 $name = "eko"; function chang_name(&$name){ $name .= '_after_change'; } chang_nam ...

  3. Linux 下 Bash配置文件读取

    Linux安装时可能要修改的配置文件:/etc/profile./etc/bashrc(ubuntu没有这个文件,对应地,其有/etc/bash.bashrc文件.我用的是ubuntu系统,所以下面将 ...

  4. oracle数据库关闭了打开数据库

    一.找到sqlplus

  5. ejs 用到的语法

    1.ejs 服务端渲染模板 2.语法: 01. <%= 变量名 %> -原样输出,不解析标签 02. <% js代码 %> 03. <%- 变量名%> -解析标签 ...

  6. javascript:控制一个元素高度始终等于浏览器高度

    window.onresize = function(){ this.opHtight()} //给浏览器添加窗口大小改变事件window.onresize = function(){ this.op ...

  7. MySQL查询优化(转)

    在分析性能欠佳的查询时,应考虑: 1) 应用程序是否正获取超过需要的数据,即访问了过多的行或列. 2) Mysql服务器是否分析了超过需要的行. 如果发现访问的数据行数很大,而生成的结果中数据行很少, ...

  8. delphi 新版内存表 FDMemTable

    c++builder XE 官方demo最全60多个 http://community.embarcadero.com/blogs?view=entry&id=8761 FireDAC.Com ...

  9. unity脚本执行顺序

    Awake ->OnEable-> Start ->-> FixedUpdate-> Update  -> LateUpdate ->OnGUI ->R ...

  10. ReactiveX 学习笔记(11)对 LINQ 的扩展

    Interactive Extensions(Ix) 本文的主题为对 Ix 库,对 LINQ 的扩展. Buffer Ix.NET Buffer Ix.NET BufferTest Buffer 方法 ...