题目意思很简单将两个有序数组合并之后的中位数找出来。题目要求使用log(m+n)的时间复杂度来做。

虽然言简意赅,但不得不承认这个题目我自己想了好久也没做出来,隐约觉得应该使用寻找第k大数的算法来做,但是具体到这个题目,编码多次都以失败告终,所以不得不去网上参考下别人的思路和代码。

参考链接:http://blog.csdn.net/zxzxy1988/article/details/8587244

但是这个方法现在已经无法在leetcode上AC,需要在两个递归的return处进行修改。

思路是这样的:

我们寻找两个数组的第k个数,那么我们首先找到两个数组中的第(k/2-1)个数。比较两个数组中这个数的大小来进行不同递归。

代码如下:

     int min(int a,int b)
{
return a>b?b:a;
}
double findknumber(int *a,int *b,int al,int bl,int k)
{
if (al > bl)
return findknumber(b, a, bl, al, k);
if (al == )
return b[k - ];
if (k == )
return min(a[], b[]);
int aindex = min(k / , al);
int bindex = k - aindex;
if (a[aindex - ] < b[bindex - ])
return findknumber(a + aindex, b, al - aindex, bindex, k - aindex);
else if (a[aindex - ] > b[bindex - ])
return findknumber(a, b + bindex, aindex, bl - bindex, k - bindex);
else
return a[aindex - ]; }
double findMedianSortedArrays(int* nums1, int nums1Size, int* nums2, int nums2Size) {
int total=nums1Size+nums2Size;
if(total%==)
return findknumber(nums1,nums2,nums1Size,nums2Size,total/+);
else
return (findknumber(nums1,nums2,nums1Size,nums2Size,total/+)
+findknumber(nums1,nums2,nums1Size,nums2Size,total/))/2.0; }

这个代码还存在两个问题,后续我再补充:

存在一个找第k个数的坐标问题;

这个算法的真实时间复杂度如何;

Median of Two Sorted Arrays-分治法的更多相关文章

  1. Kotlin实现LeetCode算法题之Median of Two Sorted Arrays

    题目Median of Two Sorted Arrays(难度Hard) 方案1,数组合并&排序调用Java方法 import java.util.* class Solution { fu ...

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

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

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

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

  4. LeetCode 4. Median of Two Sorted Arrays & 归并排序

    Median of Two Sorted Arrays 搜索时间复杂度的时候,看到归并排序比较适合这个题目.中位数直接取即可,所以重点是排序. 再来看看治阶段,我们需要将两个已经有序的子序列合并成一个 ...

  5. 【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 ...

  6. LeetCode: Median of Two Sorted Arrays 解题报告

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

  7. 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 ...

  8. 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 ...

  9. [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 ...

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

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

随机推荐

  1. poj 3269 Building A New Barn

    #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> ...

  2. python 使用xrld

    下载xrld.要对应合适的python版本: 下载tar.gz包.解压 通过cmd进入该目录. setup.py build setup.py install 安装成功: 添加路径: from sys ...

  3. iOS 发布应用时屏蔽NSLog

    在开发过程中,经常需要使用NSLog来进行调试,但是NSLog是非常影响性能的,所以我们应该在发布应用时屏蔽掉NSLog,但是如果通过手工的去一行一行的改得话,未免太枯燥与费时了,庆幸的是,我们可以通 ...

  4. (转)Log4J日志配置详解

    http://www.cnblogs.com/ITtangtang/p/3926665.html 一.Log4j简介 Log4j有三个主要的组件:Loggers(记录器),Appenders (输出源 ...

  5. JSP 和 Servlet 有哪些相同点和不同点, 他们之间的联系是什么?

    jsp和servlet的区别和联系:1.jsp经编译后就变成了Servlet.(JSP的本质就是Servlet,JVM只能识别java的类,不能识别JSP的代码,Web容器将JSP的代码编译成JVM能 ...

  6. linux怎样修改用户权限

    ls -l  查看目录下的文件详细信息 ll 同上 whoami 功能说明:先似乎用户名称. 语 法:whoami [--help][--version] 补充说明:显示自身的用户名称,本指令相当于执 ...

  7. oracle 11g 物理内存 - 此先决条件将测试系统物理内存总量是否至少为 922MB (944128.0KB)

  8. IOS 单例模式的学习

    单例模式只能修改无法释放,直到程序结束. 我们下面一步一步来做一个单例模式程序 (1)单例一旦创建,是永远存在于内存中的,所以需要创建一个全局量 static MySingletonClass *sh ...

  9. 帝国cms栏目死变量

    这里为帝国学习者们放出帝国学习者们会用到的栏目死变量,不需要灵动或者万能标签能调用,在任何位置都能使用 栏目路径:<?=$public_r[newsurl].$class_r[1]['class ...

  10. 在systemd(CentOS7)自启动zookeeper

    zookeeper的自启动脚本,如果是 sysV 模式(CeontOS6或以下版本),可以直接使用下载版本中的 src 目录下对应的 sysV 自启动包,再chkconfig即可.老方法,简单,就不说 ...