Median of Two Sorted Arrays-分治法
题目意思很简单将两个有序数组合并之后的中位数找出来。题目要求使用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-分治法的更多相关文章
- Kotlin实现LeetCode算法题之Median of Two Sorted Arrays
题目Median of Two Sorted Arrays(难度Hard) 方案1,数组合并&排序调用Java方法 import java.util.* class Solution { fu ...
- 【算法之美】求解两个有序数组的中位数 — leetcode 4. Median of Two Sorted Arrays
一道非常经典的题目,Median of Two Sorted Arrays.(PS:leetcode 我已经做了 190 道,欢迎围观全部题解 https://github.com/hanzichi/ ...
- LeetCode(3) || Median of Two Sorted Arrays
LeetCode(3) || Median of Two Sorted Arrays 题记 之前做了3题,感觉难度一般,没想到突然来了这道比较难的,星期六花了一天的时间才做完,可见以前基础太差了. 题 ...
- LeetCode 4. Median of Two Sorted Arrays & 归并排序
Median of Two Sorted Arrays 搜索时间复杂度的时候,看到归并排序比较适合这个题目.中位数直接取即可,所以重点是排序. 再来看看治阶段,我们需要将两个已经有序的子序列合并成一个 ...
- 【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 ...
- 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 ...
- 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 ...
- 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 ...
- [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 ...
- 2.Median of Two Sorted Arrays (两个排序数组的中位数)
要求:Median of Two Sorted Arrays (求两个排序数组的中位数) 分析:1. 两个数组含有的数字总数为偶数或奇数两种情况.2. 有数组可能为空. 解决方法: 1.排序法 时间复 ...
随机推荐
- Apache服务无法启动的解决方法
apache服务无法启动的解决方法 在配置apache的时候,把apache安装为服务myweb,用apacheMonitor启动myweb发现无法启动,提示:the requested operat ...
- 转:一个strcpy的问题(很容易做错)
下面的执行结果是什么? #include<stdio.h> #include<string.h> void main() { "; "; strcpy(d, ...
- c++ - fcgio.cpp:50: error: 'EOF' was not declared in this scope - Stack Overflow
c++ - fcgio.cpp:50: error: 'EOF' was not declared in this scope - Stack Overflow fcgio.cpp:50: error ...
- SSIS: 使用Lookup 和 Cache transformation 进行数据匹配简单介绍
本文将讲解Cache transformation的使用方式,并且用Lookup transformation进行匹配. 背景 如下图,我们的产品目标表中有些有尺寸信息有些没有.我们需要用Cache组 ...
- .NET连接SAP系统专题:.NET调用RFC几种方式(一)
本来今天是要写一篇关于NCO3.0的东西,就是关乎.NET调用SAP的RFC的,支持VS2010和.NET 4.0等.现在网上到处都是充斥着NCO1.X和NCO2.0,需要用VS2003来使用,都是一 ...
- windows系统操作类和演示程序(关机,关闭显示器,打开屏幕保护程序,打开光驱等)
/// <summary> /// 系统控制类,关机,关闭显示器,打开屏幕保存程序等 /// </summary> public class SystemPowerContro ...
- 不小心中了machook病毒
此文运用的是优雅的Markdown而书 前段回家过年的日子,我心爱的小air在运行时不停的弹出"machook停止运行"的提醒,上网google一下不看不要紧,才得知是mac上的一 ...
- The introduction to Web.config of ASP.NET #Reprinted#
花了点时间整理了一下ASP.NET Web.config配置文件的基本使用方法.很适合新手参看,由于Web.config在使用很灵活,可以自定义一些节点.所以这里只介绍一些比较常用的节点. <? ...
- DCI架构
提出的文章:DCI架构:一个面向对象编程的新图景 http://wenku.baidu.com/view/a7b5e401de80d4d8d15a4fed.html http://www.360doc ...
- CF 192 Div2
A.Cakeminator 暴搞之,从没有草莓覆盖的行.列遍历 char map[30][30]; int vis[30][30]; int hang[30],lie[30]; int main() ...