Median of Two Sorted Arrays (找两个序列的中位数,O(log (m+n))限制) 【面试算法leetcode】
题目:
There are two sorted arrays A and B 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)).
题意已只两个有序的序列,找到他们的中位数,复杂度要求O(log (m+n))。
问题可以转化成两个有序序列找第num大的数,用类似二分的思想,用递归处理。
因为两个序列是有序的,对比A和B第num/2个数大小,每次把小的序列删掉num/2个数,能保证不会删掉第num大的数,可以纸上验证一下。
如果一个序列没有num/2个数,那么就比较两个序列第min(n,m)个数的大小,这么做能尽快删掉一个序列所有元素,结束递归。
这么做最坏情况复杂度是O(log (m+n)),也就是num递归到1。
double find(int A[],int m,int B[],int n,int del)
{
if(m==0)return B[del-1];
else if(n==0)return A[del-1];
else if(del==1)return A[0]<B[0]?A[0]:B[0];
int temp=del/2;
if(min(m,n)<temp)temp=min(m,n);
if(A[temp-1]>=B[temp-1])return find(A,m,B+temp,n-temp,del-temp);
else return find(A+temp,m-temp,B,n,del-temp);
} class Solution {
public:
double findMedianSortedArrays(int A[], int m, int B[], int n) {
int del=(n+m+1)/2;
double temp=find(A,m,B,n,del);
if((m+n)&1)return temp;
else return (find(A,m,B,n,del+1)+temp)/2.0;
}
};
Median of Two Sorted Arrays (找两个序列的中位数,O(log (m+n))限制) 【面试算法leetcode】的更多相关文章
- 【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 tw ...
- 《LeetBook》leetcode题解(4): Median of Two Sorted Arrays[H]——两个有序数组中值问题
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- leetcode 4 : Median of Two Sorted Arrays 找出两个数组的中位数
题目: There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the ...
- 【LeetCode】4. Median of Two Sorted Arrays 寻找两个正序数组的中位数
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:数组,中位数,题解,leetcode, 力扣,python ...
- 4. Median of Two Sorted Arrays[H]两个有序数组的中位数
题目 There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the midian of the ...
- 60.Median of Two Sorted Arrays(两个排序数组的中位数)
Level: Hard 题目描述: There are two sorted arrays nums1 and nums2 of size m and n respectively. Find t ...
- 4. Median of Two Sorted Arrays(2个有序数组的中位数)
There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...
- LeetCode Median of Two Sorted Arrays 找中位数(技巧)
题意: 给两个有序(升or降)的数组,求两个数组合并之后的中位数. 思路: 按照找第k大的思想,很巧妙.将问题的规模降低,对于每个子问题,k的规模至少减半. 考虑其中一个子问题,在两个有序数组中找第k ...
- leetcode 4. Median of Two Sorted Arrays 寻找两个正序数组的中位数(困难)
一.题目大意 标签: 查找 https://leetcode.cn/problems/median-of-two-sorted-arrays 给定两个大小分别为 m 和 n 的正序(从小到大)数组 n ...
随机推荐
- 【转】Linux awk命令详解
简介 awk是一个强大的文本分析工具,相对于grep的查找,sed的编辑,awk在其对数据分析并生成报告时,显得尤为强大.简单来说awk就是把文件逐行的读入,以空格为默认分隔符将每行切片,切开的部分再 ...
- IOS Main函数
int main(int argc, char * argv[]) { @autoreleasepool { return UIApplicationMain(argc, argv, nil, NSS ...
- 用Visual Studio2010 编译 C++文件"hello world”
本周开始学习C++语言,用Visual Studio 2010做编译器,发现站内还没有基础的关于用VS2010编译程序的教材.而且自己在网上寻找时候,教程难找,而且大都不详细.故写一个关于这方面的教程 ...
- Dalvik虚拟机的运行过程分析
文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/8914953 在前面一篇文章中,我们分析了Dal ...
- POJ1742:Coins(多重背包)
Description People in Silverland use coins.They have coins of value A1,A2,A3...An Silverland dollar. ...
- html_day1
第一天学习,了解到html的结构和语法. html的语法: 1.所有的html标签都要放在<>尖括号里. 2.标签不分大小写 建议小写 3.标签中的属性与标签名之间要有一个空格,如多个 ...
- C#几种截取字符串的方法小结 (摘抄)
1.根据单个分隔字符用split截取 例如 string st="GT123_1"; string[] sArray=st.split("_"); 即可得到sA ...
- HTML5-常见的事件- beforeunload事件
当我们在日常访问某些网站时,关闭当前网页时出现如下提示: beforeunload 事件就可以完成这样的事情,该事件发生时机:页面卸载之前,可以通过它来取消卸载并继续使用原有页面. 为了显示上面弹出对 ...
- 部分GDAL工具功能简介
主要转自http://blog.csdn.net/liminlu0314?viewmode=contents 部分GDAL工具功能简介 gdalinfo.exe 显示GDAL支持的各种栅格文件的信息. ...
- FFMPEG视音频解码【一】
多媒体的时代,得多了解点编解码的技术才行,而ffmpeg为我们提供了一系列多媒体编解码的接口,如何用好这些接口达到自己所需要的目的,这也是一门重要的学问. 要是了解得不够,总是会遇到一堆又一堆问题:网 ...