题目意思很简单将两个有序数组合并之后的中位数找出来。题目要求使用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. html 实现网址链接

    <a href="http://acm.nyist.net/JudgeOnline/problemset.php">南工oj</a> HTML学习 < ...

  2. querySelector $() getElementBy区别

    参考 http://stackoverflow.com/questions/14377590/queryselector-and-queryselectorall-vs-getelementsbycl ...

  3. 查看ORACLE 数据库及表信息

    -- 查看ORACLE 数据库中本用户下的所有表 SELECT table_name FROM user_tables; -- 查看ORACLE 数据库中所有用户下的所有表 select user,t ...

  4. Date对象需要注意的点

    var today=new Date(); Date对象取得了PC内部时钟的一个快照,并同时返回一个Date对象实例. 注意静态Date对象和Date对象实例的差别,后者包含一个实际的日期值.毫秒为单 ...

  5. windows平台发消息到非UI线程.

    下面的代码是介绍如何在windows平台发消息到非UI线程. 主要是'PeekMessage || GetMessage' 这两个API的应用. 当他们被调用的时候,如果当前线程还没有消息循环,就会创 ...

  6. 《windows程序设计》学习_2.2:初识消息,双键的使用

    /* 双键的使用 */ #include <windows.h> LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM); int WINAPI ...

  7. NYOJ541 最强DE 战斗力(第五届省赛试题)

    最强DE 战斗力 时间限制:1000 ms  |  内存限制:65535 KB 难度: 描述 春秋战国时期,赵国地大物博,资源非常丰富,人民安居乐业.但许多国家对它虎视眈眈,准备联合起来对赵国发起一场 ...

  8. xcode UIView常用方法属性动画

    常见属性: @property(nonatomic,readonly) UIView *superview; 获得自己的父控件对象 @property(nonatomic,readonly,copy) ...

  9. 安卓中onBackPressed ()方法的使用

    一.onBackPressed()方法的解释 这个方法放在 void android.app.Activity.onBackPressed() 在安卓API中它是这样解释的: public void ...

  10. 【SVN】使用花生壳映射VisualSVN外网地址

    内网端口设置为443,花生壳映射设置的时候不能使用80端口,,需要用其他端口,,