LeetCode——Median of Two Sorted Arrays
Question
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)).
Example 1:
nums1 = [1, 3]
nums2 = [2]
The median is 2.0
Example 2:
nums1 = [1, 2]
nums2 = [3, 4]
The median is (2 + 3)/2 = 2.5
Solution
二分查找。 时间复杂度O(lgn)
- 将第一个数组划分成两部分
left_A | right_A
A[0], A[1], ..., A[i-1] | A[i], A[i+1], ..., A[m-1]
- 将第二个数组划分成两部分
left_B | right_B
B[0], B[1], ..., B[j-1] | B[j], B[j+1], ..., B[n-1]
- 两个左半部分小于两个右半部分
left_part | right_part
A[0], A[1], ..., A[i-1] | A[i], A[i+1], ..., A[m-1]
B[0], B[1], ..., B[j-1] | B[j], B[j+1], ..., B[n-1]
- 条件
1) len(left_part) == len(right_part)
2) max(left_part) <= min(right_part)
我们可以看到因为左右的长度一样,都等于两个数组长度之和的一半。所以i的位置确定,那么j的位置也确定了,所以我们只需要找一个合适的i,使得满足以上两个条件就可以了。
Code
class Solution {
public:
double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
int m = nums1.size();
int n = nums2.size();
if (m > n) {
vector<int> tmp = nums1;
nums1 = nums2;
nums2 = tmp;
m = nums1.size();
n = nums2.size();
}
if (m == 0) {
if (n % 2 == 1) {
return nums2[n / 2];
} else {
return ( nums2[n / 2 - 1] + nums2[n / 2] ) / 2.0;
}
}
int imin = 0;
int imax = m;
// 因为可能是奇数个,所以得加1
int half_len = (m + n + 1) / 2;
while (imin <= imax) {
int i = (imin + imax) >> 1;
int j = half_len - i;
// 说明i太大了,需要减小
if (i > 0 && nums1[i - 1] > nums2[j]) {
imax = i - 1;
}
// 说明i太小了,需要增大
else if (i < m && nums2[j - 1] > nums1[i]) {
imin = i + 1;
}
else {
int left_max;
int right_min;
// i等于0,说明nums1,全属于右边
if (i == 0) left_max = nums2[j - 1];
// j等于0,说明nums2,全属于右边
else if (j == 0) left_max = nums1[i - 1];
else left_max = max(nums1[i - 1], nums2[j - 1]);
if ((m + n) % 2 == 1)
return left_max;
// i等于m说明nums1,全属于左边
if (i == m) right_min = nums2[j];
// j等于n说明nums2,全属于左边
else if (j == n) right_min = nums1[i];
else right_min = min(nums1[i], nums2[j]);
return (left_max + right_min) / 2.0;
}
}
}
};
LeetCode——Median of Two Sorted Arrays的更多相关文章
- 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] 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 ...
- [leetcode]Median of Two Sorted Arrays @ Python
原题地址:https://oj.leetcode.com/problems/median-of-two-sorted-arrays/ 题意:There are two sorted arrays A ...
- Leetcode 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 ...
- LeetCode—— Median of Two Sorted Arrays
Description: There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the medi ...
- Leetcode: Median of Two Sorted Arrays. java.
There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted ...
- C++ Leetcode Median of Two Sorted Arrays
坚持每天刷一道题的小可爱还没有疯,依旧很可爱! 题目:There are two sorted arrays nums1 and nums2 of size m and n respectively. ...
- leetcode:Median of Two Sorted Arrays分析和实现
这个问题的大意是提供两个有序的整数数组A与B,A与B并集的中间数.[1,3]与[2]的中间数为2,因为2能将A与B交集均分.而[1,3]与[2,4]的中间数为2.5,取2与3的平均值.故偶数数目的中间 ...
- LeetCode Median of Two Sorted Arrays 找中位数(技巧)
题意: 给两个有序(升or降)的数组,求两个数组合并之后的中位数. 思路: 按照找第k大的思想,很巧妙.将问题的规模降低,对于每个子问题,k的规模至少减半. 考虑其中一个子问题,在两个有序数组中找第k ...
随机推荐
- 多线程入门-第五章-线程的调度与控制之yield
yield与sleep类似,只是不能指定暂停多长时间,并且只能让同优先级的线程有执行的机会,让位时间不固定. /* yield使用 */ public class ThreadTest04 { pub ...
- Java SAX handle xml
https://www.journaldev.com/1198/java-sax-parser-example Java SAX Parser Example SAX Parser in java ...
- Zipline入门教程
Zipline Beginner Tutorial Basics 基础 Zipline is an open-source algorithmic trading simulator written ...
- Ubuntu 12.04安装Google Chrome(转)
下载google chrome deb包,下载地址:https://www.google.com/chrome/browser/desktop/index.html,google的网站被墙了,如果你下 ...
- mysql之引擎、Explain、权限详解
引擎 简介 Innodb引擎 Innodb引擎提供了对数据库ACID事务的支持,并且实现了SQL标准的四种隔离级别.该引擎还提供了行级锁和外键约束,它的设计目标是处理大容量数据库系统,它本身其实就是基 ...
- 【我的Android进阶之旅】 RxJava 理解Backpressure并解决异常 rx.exceptions.MissingBackpressureException
今天测试人员在测试应用APP的时候应用crash了,查看了下crash log如下所示: java.lang.IllegalStateException: Exception thrown on Sc ...
- 如何制作一款HTML5 RPG游戏引擎——第三篇,利用幕布切换场景
开言: 在RPG游戏中,如果有地图切换的地方,通常就会使用幕布效果.所谓的幕布其实就是将两个矩形合拢,直到把屏幕遮住,然后再展开直到两个矩形全部移出屏幕. 为了大家做游戏方便,于是我给这个引擎加了这么 ...
- cpu-》内存-》磁盘
cpu相当于计算机大脑负责计算以及发送执行命令:内存相当于人的记忆是临时存储:磁盘相当于笔记本,负责永久存储数据: 当系统需要调用硬盘当中的数据时,会将硬盘数据读入内存供cpu进行处理.cpu只会读取 ...
- python全栈开发从入门到放弃之内置函数
1.locals.globals def func(): x = 1 y = 2 print(locals()) #打印局部作用域中的名字 print(globals()) #打印全局作用域中的名字 ...
- 以About Us为范例在Zen cart中增加页面
1.在includes\languages\english\html_includes目录中新建文件define_about_us.php 2.在includes\templates\Your_tem ...