LeetCode题解-----Median of Two Sorted Arrays
题目描述:
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)).
解题思路:
本题要求求解的是两个有序序列的中位数。本质上就是求两个有序序列“第k小的数“的变形。假设两个有序序列一个长为m,另一个长为n,则我们要求的就是第(m+n)/2+1个数,若m+n为偶数,则求的是第(m+n)/2和第(m+n)/2+1个数的平均数。
接下来分析如何在O(log(m+n))的复杂度内求解第k个小的数。我们首先假设k为偶数并且两个有序序列a,b的长度都大于k/2(边界情况见代码),比较a[k/2-1]和b[k/2-1]的大小:
1)若a[k/2-1]==b[k/2-1],则该值就是我们所要求的值,因为将a和b的前k/2个元素归并后就获得了a,b序列的前k个元素,并且a[k/2-1]和b[k/2-1]相等且在最末尾。
2)若a[k/2-1]<b[k/2-1],则a的前k/2个元素中并不包含我们所求的第k小的元素,因此我们可以将其舍弃,进而递归求解剩下这些元素的第(k-k/2)小的元素。
3)若a[k/2-1]>b[k/2-1],处理方法和情况2类似
复杂度分析:
我们在求解第k小的元素的每次递归的过程中,基本上每次都要舍弃接近k/2的元素,而k的初始值为(m+n)/2,因为算法的复杂度为O(log(m+n))
代码:
int findkth(int* a,int aSize,int*b,int bSize,int k) {
int aPos,bPos;
if(aSize>bSize){//保证a始终是较短序列
return findkth(b,bSize,a,aSize,k);
}
if(aSize==0){//如果序列a空了,则直接返回
return b[k-1];
}
if(k==1){
return a[0]<b[0] ? a[0] : b[0];
}
aPos = k/2<aSize ? k/2 : aSize;//如果a太短,则直接取a的末尾元素比较
bPos = k-aPos;
if(a[aPos-1]==b[bPos-1]){
return a[aPos-1];
}else if(a[aPos-1]<b[bPos-1]){
return findkth(a+aPos,aSize-aPos,b,bSize,k-aPos);
}else{
return findkth(a,aSize,b+bPos,bSize-bPos,k-bPos);
}
}
double findMedianSortedArrays(int* nums1, int nums1Size, int* nums2, int nums2Size) {
if((nums1Size+nums2Size)%2){
return findkth(nums1,nums1Size,nums2,nums2Size,(nums1Size+nums2Size)/2+1)*1.0;
}else{
return (findkth(nums1,nums1Size,nums2,nums2Size,(nums1Size+nums2Size)/2)
+findkth(nums1,nums1Size,nums2,nums2Size,(nums1Size+nums2Size)/2+1))/2.0;
}
}
LeetCode题解-----Median of Two Sorted Arrays的更多相关文章
- LeetCode题解——Median of Two Sorted Arrays
题目: 找两个排序数组A[m]和B[n]的中位数,时间复杂度为O(log(m+n)). 解法: 更泛化的,可以找第k个数,然后返回k=(m+n)/2时的值. 代码: class Solution { ...
- 【算法之美】求解两个有序数组的中位数 — 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(二分)
4. Median of Two Sorted Arrays 题目链接:https://leetcode.com/problems/median-of-two-sorted-arrays/ Descr ...
- LeetCode 4 Median of Two Sorted Arrays (两个数组的mid值)
题目来源:https://leetcode.com/problems/median-of-two-sorted-arrays/ There are two sorted arrays nums1 an ...
- LeetCode 4. Median of Two Sorted Arrays & 归并排序
Median of Two Sorted Arrays 搜索时间复杂度的时候,看到归并排序比较适合这个题目.中位数直接取即可,所以重点是排序. 再来看看治阶段,我们需要将两个已经有序的子序列合并成一个 ...
- 第三周 Leetcode 4. Median of Two Sorted Arrays (HARD)
4. Median of Two Sorted Arrays 给定两个有序的整数序列.求中位数,要求复杂度为对数级别. 通常的思路,我们二分搜索中位数,对某个序列里的某个数 我们可以在对数时间内通过二 ...
- 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 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 ...
随机推荐
- 与众不同 windows phone (50) - 8.1 新增控件: PickerFlyout, ListPickerFlyout
[源码下载] 与众不同 windows phone (50) - 8.1 新增控件: PickerFlyout, ListPickerFlyout 作者:webabcd 介绍与众不同 windows ...
- Fluent Nhibernate and Stored Procedures
sql:存储过程 DROP TABLE Department GO CREATE TABLE Department ( Id INT IDENTITY(1,1) PRIMARY KEY, DepNam ...
- LeetCode127:Word Ladder II
题目: Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) ...
- Win7配置Go环境
最近想学习下Go语言,先从最基本的Hello Go开始,搭建Go开发环境 一.下载Go包 由于Go官网国内访问经常有问题,可以从国内的镜像下载: http://www.golangtc.com/ 二. ...
- C# Sqlite 序列
sqlite 不能直接创建自定义函数,不能像 sql server中那样方便创建并使用.不过我们照样可以创建它,创建成功后,我们照样可以随心所欲(比如批量更新等) 序列是一个数据库中很常用的操作,在其 ...
- apple store链接格式文档
备份一下: The app on Appstore has specific URL format http://itunes.apple.com/[country-code]/app/[app-na ...
- Underscore学习笔记1
项目用了很久underscore.每次都是临时查手册,没有系统的研究过,最近有空正好看看 github地址:https://github.com/lily1010/underscore_learn 一 ...
- SAP debug的几种方式
1. 直接在程序中设断点 这种方式想必大家都知道了,在se38里面打上breakpoint,程序运行到该处即进入debug模式 2.background Job的debug 进入SM37 ...
- Displaying SharePoint Lists or Libraries in other sites 显示其他站点的List
Child objects within SharePoint, like a list in a Site, share an inherent connection with that Paren ...
- 【读书笔记】iOS-UIFont-动态下载系统提供的字体-官方代码
一,工程目录 二,AppDelegate.m - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOption ...