leetcode-algorithms-4 Median of Two Sorted Arrays
leetcode-algorithms-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 two sorted arrays. The overall run time complexity should be O(log (m+n)).
You may assume nums1 and nums2 cannot be both empty.
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
解法
left | right
a[0] ... a[i-1] | a[i] ... a[m]
b[0] ... b[j-1] | b[j] ... b[n]
对于两个数组,如上所示,保证max(left) <= min(right)同时左边数的个数为中位数个数即(m+n+1)/2.就可以找到中位数的值.
可先取a数组i = m/2,那j = mid(中位数) - i;如果a[i - 1]大于b[j]不能保证max(left) <= min(right)就需要把a的扣掉一个,b[j - 1]和a[i]也是一样的情况.当上面两种都不符合时,就说明已经找到中位数的位置.
class Solution
{
public:
double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2)
{
int l1 = nums1.size();
int l2 = nums2.size();
int l = l1 + l2;
if (l1 > l2)
{
nums1.swap(nums2);
l1 = l - l1;
l2 = l - l1;
}
int mid = (l + 1) / 2;
int min = 0;
int max = l1;
while(min <= max)
{
int i = (min + max) / 2;
int j = mid - i;
if (i < max && nums2[j - 1] > nums1[i])
min = i + 1;
else if (i > min && nums1[i - 1] > nums2[j])
max = i - 1;
else
{
int left = 0;
if (i == 0)
left = nums2[j - 1];
else if (j == 0)
left = nums1[i - 1];
else
left = nums2[j - 1] > nums1[i - 1] ? nums2[j - 1] : nums1[i - 1];
if (l % 2 != 0)
return left;
int right = 0;
if (i == l1)
right = nums2[j];
else if (j == l2)
right = nums1[i];
else
right = nums2[j] > nums1[i] ? nums1[i] : nums2[j];
return double(left + right) / 2.0;
}
}
return 0.0;
}
};
时间复杂度: O(log(m+n)).
空间复杂度: O(1).
leetcode-algorithms-4 Median of Two Sorted Arrays的更多相关文章
- 【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 ...
- 《LeetBook》leetcode题解(4): Median of Two Sorted Arrays[H]——两个有序数组中值问题
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- leetcode.C.4. Median of Two Sorted Arrays
4. Median of Two Sorted Arrays 这应该是最简单最慢的方法了,因为本身为有序,所以比较后排序再得到中位数. double findMedianSortedArrays(in ...
- 【LeetCode】4. Median of Two Sorted Arrays 寻找两个正序数组的中位数
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:数组,中位数,题解,leetcode, 力扣,python ...
- 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 Array 4 Median of Two Sorted Arrays
做leetcode题目的第二天,我是按照分类来做的,做的第一类是Array类,碰见的第二道题目,也就是今天做的这个,题目难度为hard.题目不难理解,但是要求到了时间复杂度,就需要好好考虑使用一下算法 ...
- 【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 t ...
- LeetCode OJ 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 two ...
随机推荐
- (转)How Hash Algorithms Work
本文转自:http://www.metamorphosite.com/one-way-hash-encryption-sha1-data-software Home Posted: Novembe ...
- mysql分区分表讲解
为什么要分表和分区? 日常开发中我们经常会遇到大表的情况,所谓的大表是指存储了百万级乃至千万级条记录的表.这样的表过于庞大,导致数据库在查询和插入的时候耗时太长,性能低下,如果涉及联合查询的情况,性能 ...
- WebGIS前端地图显示之根据地理范围换算出瓦片行列号的原理(核心)
文章版权由作者李晓晖和博客园共有,若转载请于明显处标明出处:http://www.cnblogs.com/naaoveGIS/. 1.前言 在上一节中我们知道了屏幕上一像素等于实际中多少单位长度(米或 ...
- layer 弹出层 回调函数调用 弹出层页面 函数
1.项目中用到layer 弹出层,定义一个公用的窗口,问题来了窗口弹出来了,如何保存页面上的数据呢?疯狂百度之后,有了结果,赶紧记下. 2.自己定义的公共页面方法: layuiWindow: func ...
- Redux基础使用
Redux基础使用: 简介:这里是从需求来响应的执行操作redux,所以理解起来更加的容易铭记在心的三点:action/reducer/store 除此之外就是react/react native的基 ...
- pom中配置的仓库无效的问题
今天在用spring cloud的时候发现,配置的pom仓库一直无效(官网要求2.0版本直接从指定仓库里下).于是上网搜索,发现(http://18810098265.iteye.com/blog/2 ...
- 获取指定tag的代码
git checkout v1.0.3 再使用ls查看就可以了
- 能改变this各种情况下的总结,还有没有总结到的,请留言!!
1.. 在函数参数中的,回调函数的this ,指向window 如: promise中的回调函数, 可以在方法外,转存this 2..构造函数中,this指向,实例对象 , 在全局中this是win ...
- nRF52832的SAADC
SAADC部分思维导图 1ADC原理 1.1主要特点 1)8/10/12分辨率,使用过采样可达到14位分辨率 2)多达8个通道 单端输入时使用1个通道,2个通道可组成差分输入 单端和差分输入时均可配置 ...
- Spring Cloud 入门教程(一): 服务注册
1. 什么是Spring Cloud? Spring提供了一系列工具,可以帮助开发人员迅速搭建分布式系统中的公共组件(比如:配置管理,服务发现,断路器,智能路由,微代理,控制总线,一次性令牌,全局锁 ...