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 目录

leetcode-algorithms-4 Median of Two Sorted Arrays的更多相关文章

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

  2. 《LeetBook》leetcode题解(4): Median of Two Sorted Arrays[H]——两个有序数组中值问题

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

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

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

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

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

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

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

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

  7. 【LeetCode OJ】Median of Two Sorted Arrays

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

  8. Leetcode Array 4 Median of Two Sorted Arrays

    做leetcode题目的第二天,我是按照分类来做的,做的第一类是Array类,碰见的第二道题目,也就是今天做的这个,题目难度为hard.题目不难理解,但是要求到了时间复杂度,就需要好好考虑使用一下算法 ...

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

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

随机推荐

  1. 【.Net】在windows server 2016 和Windows10这些server上安装.net fw3.5

    一般就是打开server manager. 一直next到add feature 讲net3.5勾选 发现需要指定一个路径是什么 source\sxs之类的 下载microsoft-windows-n ...

  2. VirtuablBox 出错: VERR_SUPLIB_OWNER_NOT_ROOT 解决方法

    刚刚把 VirtualBox 升级, 从 3.2 到 4.0.4 后,虚拟机上的系统无法运行, 提示: VERR_SUPLIB_OWNER_NOT_ROOT 查了一下,发现是因为 /opt 的 own ...

  3. 转载:linux tar 解压命令总结

    把常用的tar解压命令总结下,当作备忘: tar -c: 建立压缩档案 -x:解压 -t:查看内容 -r:向压缩归档文件末尾追加文件 -u:更新原压缩包中的文件 这五个是独立的命令,压缩解压都要用到其 ...

  4. VHDL 例程

    以下程序未经仿真,仅供说明 语法 声明参考库ieee,使用ieee中的std_logic_1164包全部条目可见 library ieee; use ieee.std_logic_1164.all; ...

  5. FPGA 概述2

    参考1 参考2:浅论各种调试接口(SWD.JTAG.Jlink.Ulink.STlink)的区别 以下数据仅供参考 文章概要 主流FPGA厂商及产品 相同设计在FPGA与ASIC中耗费器件数量比较 F ...

  6. 4、python内置类型(0529)

    支持运算:索引,切片,min(), max(), len()等 支持操作:对象的自有的方法 对字符串操作的内置方法获取:str.     //敲tab键补全 获取某个内建命令的属性和方法列表:dir( ...

  7. Jenkins参数化构建(三)之 Jenkins从文件中读取运行参数

    安装Extended Choice Parameter插件 选择‘参数化构建过程’ maven command line中进行引用 clean test -DsuiteXmlFile=src/main ...

  8. R的极客理想系列文章--转载

    http://blog.fens.me/series-r/ R的极客理想系列文章,涵盖了R的思想,使用,工具,创新等的一系列要点,以我个人的学习和体验去诠释R的强大. R语言作为统计学一门语言,一直在 ...

  9. 每日质量NPM包事件绑定_bindme(详解React的this)

    一.bindme 官方定义: is a helper to bind a list of methods to an object reference 理解: 因为不推荐在render()里构建函数, ...

  10. _itemmod_strengthen_item

    `enchant_id`升级后的附魔Id `prev_enchant_id` 上级附魔Id `description` 描述,出现在菜单中 `enchantReqId`升级附魔效果的消耗模板 `rem ...