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.排序法 时间复 ...
随机推荐
- Android UI--ViewPager扩展Tab标签指示
Android UI--ViewPager扩展Tab标签指示 2013年8月30日出来冒冒泡 ViewPager这个控件已经不算是陌生的了,各种玩Android的小伙伴们都有发表相应的文章来讲它.我看 ...
- Cocos2d-x:环境配置小节
一.准备 须要下载下面内容. 1. vs2010 下载地址:http://download.microsoft.com/download/1/4/3/143B7583-6225-474F-88D5-5 ...
- BingMap的GeocodeService进行地理位置正向和反向检索--后台实现
一.加入GeocodeService的Web服务引用 地理编码服务(GeocodeService)是以WCF技术公布的一个Web服务,地图编码服务提供了以一个有效的物理地址在地图上匹配其相应的地图地址 ...
- 2.Freshman阶段学习内容的确定
我刷知乎.在知乎上答题的程序员,不是很牛逼就是更牛逼,说起各种系统.各种系统的各种版本.各种语言.数据库.算法.IT届的各种圣战都有板有眼.信手拈来.头头是道,不得不服.这导致了一些非常严重的问题:我 ...
- java读取配置文件的几种方法
java读取配置文件的几种方法 原文地址:http://hbcui1984.iteye.com/blog/56496 在现实工作中,我们常常需要保存一些系统配置信息,大家一般都会选择配 ...
- Stack(栈)
Stack(栈)是一种后进先出的数据结构,下面介绍一下栈的具体运用: 一.Stack 中的 empty 函数 stack<int> s( 5 , 10) ; s.empty() ; ...
- BZOJ 1207: [HNOI2004]打鼹鼠( dp )
dp.. dp[ i ] = max( dp[ j ] + 1 ) ------------------------------------------------------------------ ...
- [C#编程参考]把图像转换为数组的两种实现
当一个程序和一个图片放在一起,无非有两种操作: 第一种,就是传输这个图片,在传输图片之前要首先把这个图片变成byte类型的数组.所以这时候我们用到的是图片的存储的数据,也就是图片属性中的大小.我们并不 ...
- Java学习之finally
如果catch中有return语句,finally里面的语句还会执行吗? 会执行,在return语句的中间执行 public class Test{ public static void main(S ...
- 自定义jquery手风琴插件
手风琴效果是项目中使用频率较高的一种效果,原来项目一直都在用easyui的,临近年末,试着自己写了一个 css样式 /* CSS Document */ body { margin: 0 auto; ...