《LeetBook》leetcode题解(4): Median of Two Sorted Arrays[H]——两个有序数组中值问题
我现在在做一个叫《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]——两个有序数组中值问题的更多相关文章
- 【LeetCode】4. Median of Two Sorted Arrays 寻找两个正序数组的中位数
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:数组,中位数,题解,leetcode, 力扣,python ...
- leetcode 4. Median of Two Sorted Arrays 寻找两个正序数组的中位数(困难)
一.题目大意 标签: 查找 https://leetcode.cn/problems/median-of-two-sorted-arrays 给定两个大小分别为 m 和 n 的正序(从小到大)数组 n ...
- 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 ...
- 【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.C.4. Median of Two Sorted Arrays
4. Median of Two Sorted Arrays 这应该是最简单最慢的方法了,因为本身为有序,所以比较后排序再得到中位数. double findMedianSortedArrays(in ...
- 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 ...
- 【一天一道LeetCode】#4 Median of Two Sorted Arrays
一天一道LeetCode (一)题目 There are two sorted arrays nums1 and nums2 of size m and n respectively. Find th ...
- 【LeetCode OJ】Median of Two Sorted Arrays
题目链接:https://leetcode.com/problems/median-of-two-sorted-arrays/ 题目:There are two sorted arrays nums1 ...
- 【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 ...
随机推荐
- MySQL性能调优与架构设计——第10章 MySQL数据库Schema设计的性能优化
第10章 MySQL Server性能优化 前言: 本章主要通过针对MySQL Server(mysqld)相关实现机制的分析,得到一些相应的优化建议.主要涉及MySQL的安装以及相关参数设置的优化, ...
- Linux 基础教程 43-su和sudo命令
在使用Linux系统中,有时候还需要做身份切换,这是为什么? 使用普通账号:系统日常操作的好习惯 虽然使用root对系统进行各种操作不受权限等方面的限制,但却存在重大的安全隐患,假如有人不 ...
- Mybatis 模糊查询 like【笔记】Could not set parameters for mapping
当使用mybatis 做模糊查询时如果这样写 会报 Could not set parameters for mapping: ParameterMapping{property='keywords' ...
- 在DOS命令行窗口中显示系统环境环境变量
(这是一个小技巧) 示例命令: echo %path% path是系统环境变量,使用百分号包围起来 http://www.cnblogs.com/danzhang 张洪君 微软ALM MVP
- 连接池--sp_reset_connection
--当客户端使用连接池访问数据库时,客户端使用OPEN来重用数据库连接,使用CLOSE来断开数据库连接,但并不物理上新建和断开连接,因此可以提高程序运行速度并降低性能损耗. --ADO和ADO.NET ...
- SelectOnCheck
1.checkOnSelect 如果为true,当用户点击行的时候该复选框就会被选中或取消选中. 如果为false,当用户仅在点击该复选框的时候才会呗选中或取消. 2.selectOnCheck 如果 ...
- Vsual Studio 高版本打开低版本项目的方法
https://www.cnblogs.com/jmliao/p/5594179.html
- css细节复习笔记——内边距、边框和外边距
一个元素的内边距.边框和外边距属性会影响着整个文档如何布局,更重要的是,它们会严重影响给定元素的外观. 高度和宽度 这两个属性不能应用到行内非替换元素,其高度和宽度由链接的内容确定,而不是由创作人员确 ...
- DOS磁盘操作类命令
外部命令 formAT---磁盘格式化命令 格式:formAT<盘符:>[/S][4][/Q] 1)命令收的盘符不可缺省,若对硬盘进行格式化,则会有如下提示:WARNING:ALL DA ...
- 使用textarea标签代替input标签可以实现输入框的大小调节,自动换行,滚动条显示
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...