我现在在做一个叫《leetbook》的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看
书的地址:https://hk029.gitbooks.io/leetbook/

004. Median of Two Sorted Arrays[H]

题目

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)).

分析

这个题目是非常的常见,而且有特别多的变形。特别是在当前大数据的环境下,如何快速查找第i个元素有很现实的意义。如果想看分治的思路,建议直接看这个文章:【分步详解】两个有序数组中的中位数和Top K问题

思路1——遍历合并数组

很简单的思路:就是遍历两个数组,在里面找到第i个大元素,这个应该还是比较简单的,时间复杂度O(m+n)。

用2个变量分别指向两个数组,每次取较小的一个,然后将其指针后移动。但是这里有个问题,就是奇偶判断,如果是奇数,中位数是num[mid],但是如果是偶数,是(num[mid]+num[mid-1])/2。这里我的做法是把num[mid]看作(num[mid]+num[mid])/2。如果是偶数-1,奇数-0。

class Solution {
public:
double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
if(nums1.size() == 0)
return MedofArray(nums2);
if(nums2.size() == 0)
return MedofArray(nums1);
vector<int> num3;
int size = (nums1.size()+nums2.size());
int mid = size/2;
int flag = !(size%2);
int i,m1,m2,cur;
double a,b;
for(i = m1 = m2 = 0;i < size;i++)
{
a = m1 < nums1.size()?nums1[m1]:INT_MAX;//过界处理
b = m2 < nums2.size()?nums2[m2]:INT_MAX;//过界处理
//cout<<i<<" a "<<a<<" b "<<b<<endl;
if(a < b)
{
num3.push_back(nums1[m1]);
m1++;
}
else
{
num3.push_back(nums2[m2]);
m2++;
}
if(i == mid)
break;
}
return (num3[mid]+num3[mid-flag])/2.0;
}
double MedofArray(vector<int>& nums)
{
int mid = nums.size()/2;
int flag = !(nums.size()%2);
return (nums[mid]+nums[mid-flag])/2.0;
}
};

思路2——分治 *

重点来了!!
这是一个很经典的Divide & Conquer的题目,关键就在如何划分。这里引用stellari 的高分答案,觉得他这个讲的特别好:
由于篇幅很长,我把这个移动到了这篇文章: 【分步详解】两个有序数组中的中位数和Top K问题

代码

class Solution {
public:
double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
int n = nums1.size();
int m = nums2.size();
if(n > m) //保证数组1一定最短
return findMedianSortedArrays(nums2,nums1);
int L1,L2,R1,R2,c1,c2,lo = 0, hi = 2*n; //我们目前是虚拟加了'#'所以数组1是2*n长度
while(lo <= hi) //二分
{
c1 = (lo+hi)/2; //c1是二分的结果
c2 = m+n- c1;
L1 = (c1 == 0)?INT_MIN:nums1[(c1-1)/2]; //map to original element
R1 = (c1 == 2*n)?INT_MAX:nums1[c1/2];
L2 = (c2 == 0)?INT_MIN:nums2[(c2-1)/2];
R2 = (c2 == 2*m)?INT_MAX:nums2[c2/2]; if(L1 > R2)
hi = c1-1;
else if(L2 > R1)
lo = c1+1;
else
break;
}
return (max(L1,L2)+ min(R1,R2))/2.0;
}
};

《LeetBook》leetcode题解(4): Median of Two Sorted Arrays[H]——两个有序数组中值问题的更多相关文章

  1. 【LeetCode】4. Median of Two Sorted Arrays 寻找两个正序数组的中位数

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:数组,中位数,题解,leetcode, 力扣,python ...

  2. leetcode 4. Median of Two Sorted Arrays 寻找两个正序数组的中位数(困难)

    一.题目大意 标签: 查找 https://leetcode.cn/problems/median-of-two-sorted-arrays 给定两个大小分别为 m 和 n 的正序(从小到大)数组 n ...

  3. 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 ...

  4. 【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 ...

  5. leetcode.C.4. Median of Two Sorted Arrays

    4. Median of Two Sorted Arrays 这应该是最简单最慢的方法了,因为本身为有序,所以比较后排序再得到中位数. double findMedianSortedArrays(in ...

  6. leetcode第二题--Median of Two Sorted Arrays

    Problem:There are two sorted arrays A and B of size m and n respectively. Find the median of the two ...

  7. 【一天一道LeetCode】#4 Median of Two Sorted Arrays

    一天一道LeetCode (一)题目 There are two sorted arrays nums1 and nums2 of size m and n respectively. Find th ...

  8. 【LeetCode OJ】Median of Two Sorted Arrays

    题目链接:https://leetcode.com/problems/median-of-two-sorted-arrays/ 题目:There are two sorted arrays nums1 ...

  9. 【LeetCode】004. Median of Two Sorted Arrays

    题目: There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the ...

随机推荐

  1. hdu5340—Three Palindromes—(Manacher算法)——回文子串

    Three Palindromes Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others ...

  2. HDU 5974 A Simple Math Problem(数论+结论)

    Problem Description Given two positive integers a and b,find suitable X and Y to meet the conditions ...

  3. 开源WebGIS实施方案(四):GeoServer发布PostGIS数据

    GeoServer可以支持多种格式的数据源,本文只介绍其中一种,即PostGIS数据源. 新建一个工作区.工作区这个名字也是一变再变,早前叫做目录,后来改为工作空间,如今已变为工作区了. 添加数据存储 ...

  4. 自定义Team Foundation Server (TFS) 与Project Professional的集成字段

    用户可以象使用Office Excel一样,使用Project Professional连接TFS,将数据下载到本地修改,并且发布到TFS服务器上,如果你习惯使用Project来计划你的项目,那么Pr ...

  5. Gogland编译Syncthing!

    说明:我仅仅以这个Syncthing工程为例,来说明如何正确使用Goland编译其他人写的工程,应该具有普遍意义,看懂这篇博客,你想用Gogland去编译其他人的工程,应该不是问题!! Syncthi ...

  6. 深入了解java虚拟机(JVM) 第八章 常见的jvm调优策略

    一般来说,jvm的调优策略是没有一种固定的方法,只有依靠我们的知识和经验来对项目中出现的问题进行分析,正如吉德林法则那样当你已经把问题清楚写出来,就已经解决了一半.虽然JVM调优中没有固定的策略,但是 ...

  7. 《快学Scala》第八章 继承

  8. 关于CentOS-6的默认带的mysql启动和安装问题

    http://blog.csdn.net/arrowzz/article/details/24439731 以下纯复制粘贴: 一开始想自己一步一步从编译开始搭建一个lanmp环境: 从鸟哥的linux ...

  9. 利用PHP的curl批量导出文件

    场景 1.需要从网站中导出大量的文件,包括有图片格式,文档格式等附件内容. 2.分文件夹来导出不同的数据 <?php header("Content-Type:text/html;ch ...

  10. html中object和embed标签的区别

    ♦object定义一个嵌入的对象.请使用此元素向您的 XHTML 页面添加多媒体.此元素允许您规定插入 HTML 文档中的对象的数据和参数,以及可用来显示和操作数据的代码. ♦<object&g ...