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 ...
随机推荐
- Nagle's algorithm
w41字节的数据包只有1字节的可用信息.以减少数据包发送量来提高TCP/IP网络性能. https://en.wikipedia.org/wiki/Nagle's_algorithm https:// ...
- php和jsCOOKIE实现前端交互
w如何精简? <script> document.cookie = 'wjs_cookie=' + 'amz_reviews'; function w(id) { document.coo ...
- 属性attribute和property的区别
<!DOCTYPE html> <html> <head> <meta http-equiv="content-type" content ...
- 剑指Offer——矩阵中的路径
题目描述: 请设计一个函数,用来判断在一个矩阵中是否存在一条包含某字符串所有字符的路径.路径可以从矩阵中的任意一个格子开始,每一步可以在矩阵中向左,向右,向上,向下移动一个格子.如果一条路径经过了矩阵 ...
- Go学习笔记一:解析toml配置文件
本文系作者原创,转载请注明出处https://www.cnblogs.com/sonofelice/p/9085291.html . 一些mysql或者日志路径的信息需要放在配置文件中.那么本博文主要 ...
- Python 装饰器的诞生过程
Python中的装饰器是通过利用了函数特性的闭包实现的,所以在讲装饰器之前,我们需要先了解函数特性,以及闭包是怎么利用了函数特性的 ① 函数特性 Python中的函数特性总的来说有以下四点: 1. ...
- pendingIntent的FLAG标签:
PendingIntent是一个特殊的Intent,实际上它像一个邮包,其中包裹着真正的Intent,当邮包未打开时,Intent是被“挂起”的,所以并不执行, 只有当邮包拆开时才会执行.它与Inte ...
- Oracle 学习笔记 12 -- 序列、索引、同义词
版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/Topyuluo/article/details/24232449 数据库的对象包含:表.视图.序列. ...
- python爬虫 (一) 爬虫基础了解,urllib
• URL的含义: 统一资源定位符,结构: URL的格式由三部分组成: ①第一部分是协议(或称为服务方式). ②第二部分是存有该资源的主机IP地址(有时也包括端口号). ③第三部分是主机资源的具体地址 ...
- HDU 2191 珍惜现在,感恩生活(多重背包模板题)
多重背包模板题 #include<iostream> #include<cstring> #include<algorithm> using namespace s ...