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
class Solution {
public:
/*
对于一个长度为n的已排序数列a,若n为奇数,中位数为第(n/2+1)个数 ,
若n为偶数,则中位数=[第(n/2)个数 + 第(n/2+1)个数] / 2
如果我们可以在两个数列中求出第K小的元素,便可以解决该问题
不妨设数列A元素个数为n,数列B元素个数为m,各自升序排序,求第k小元素
取A[k / 2] B[k / 2] 比较,
如果 A[k / 2] > B[k / 2] 那么,所求的元素必然不在B的前k / 2个元素中(证明反证法)
反之,必然不在A的前k / 2个元素中,于是我们可以将A或B数列的前k / 2元素删去,求剩下两个数列的
k - k / 2小元素,于是得到了数据规模变小的同类问题,递归解决
如果 k / 2 大于某数列个数,所求元素必然不在另一数列的前k / 2个元素中,同上操作就好。
*/
double findKth(vector<int>& A, vector<int>& B, int A_st, int B_st, int k) { //经典函数
// 边界情况,任一数列为空
if (A_st >= A.size()) {
return B[B_st + k - ];
}
if (B_st >= B.size()) {
return A[A_st + k - ];
}
// k等于1时表示取最小值,直接返回min
if (k == ) return min(A[A_st], B[B_st]);
int A_key = A_st + k / - >= A.size() ? INT_MAX : A[A_st + k / - ];
int B_key = B_st + k / - >= B.size() ? INT_MAX : B[B_st + k / - ];
if (A_key < B_key){
return findKth(A, B, A_st + k / , B_st, k - k / );
} else {
return findKth(A, B, A_st, B_st + k / , k - k / );
} }
double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
int sum = nums1.size() + nums2.size();
double ret; if (sum & ) {
ret = findKth(nums1, nums2, , , sum / + );
} else {
ret = ((findKth(nums1, nums2, , , sum / )) +
findKth(nums1, nums2, , , sum / + )) / 2.0;
}
return ret;
}
};

【medium】4. Median of Two Sorted Arrays 两个有序数组中第k小的数的更多相关文章

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

  2. [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 the two ...

  3. 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 two ...

  4. [LintCode] 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 ...

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

  6. 2.Median of Two Sorted Arrays (两个排序数组的中位数)

    要求:Median of Two Sorted Arrays (求两个排序数组的中位数) 分析:1. 两个数组含有的数字总数为偶数或奇数两种情况.2. 有数组可能为空. 解决方法: 1.排序法 时间复 ...

  7. Leetcode4.Median of Two Sorted Arrays两个排序数组的中位数

    给定两个大小为 m 和 n 的有序数组 nums1 和 nums2 . 请找出这两个有序数组的中位数.要求算法的时间复杂度为 O(log (m+n)) . 你可以假设 nums1 和 nums2 不同 ...

  8. 4. Median of Two Sorted Arrays(2个有序数组的中位数)

    There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...

  9. 4. Median of Two Sorted Arrays *HARD* -- 查找两个排序数组的中位数(寻找两个排序数组中第k大的数)

    There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...

随机推荐

  1. Centos 7(linux)系统下如何给jar应用程序创建桌面快捷方式

    1.创建系统自带的应用程序快捷方式 对于系统自带的应用程序,其桌面快捷方式存储的位置为以下三个目录中的其中一个: /usr/share/applications ~/.local/share/appl ...

  2. Python两个栈实现一个队列

    牛客网原题: 用两个栈来实现一个队列,完成队列的Push和Pop操作. 队列中的元素为int类型.   实现这个算法的方式有很多种,这里就写一种比较简单易懂的:虽然可能算法和效率上不太出色,当大多数人 ...

  3. MySQL-悲观锁和乐观锁

    引言  悲观锁和乐观锁指的并不是一种锁,而是一种思想,一种并发控制的方法. 在事务并发执行的情景中,可能存在多个用户同时更新同一条数据的情况,这可能会产生冲突导致丢失更新或者脏读. 丢失更新是指一个事 ...

  4. Python scrapy爬虫数据保存到MySQL数据库

    除将爬取到的信息写入文件中之外,程序也可通过修改 Pipeline 文件将数据保存到数据库中.为了使用数据库来保存爬取到的信息,在 MySQL 的 python 数据库中执行如下 SQL 语句来创建 ...

  5. tp5问题整理

    问题一:致命错误: Class 'think\controller' not found 原因:controller首字母要大写 解决:use think\Controller; 问题二:html页面 ...

  6. 数组中的reduce 函数理解

    第一次见到reduce 是在js 的高级程序设计中,它的意思是把一个数组减少为一个数,举的例子是数组中元素的求和.它接受一个函数作为参数,函数又有两个参数,一个是prev, 前一个值,一个是next, ...

  7. [2019.04.01]Linux 学习心得(2)-- tar 命令的理解

    这篇文章并不是发布最早的但是阅读量却每天都见长,很想知道各位大大是怎么找到这篇文章的.如果不忙,还请各位大大评论一下我看看,没准我可以为大家改进一下本文,提升一下质量. =============== ...

  8. 《JAVA与模式》之工厂方法模式

    在阎宏博士的<JAVA与模式>一书中开头是这样描述工厂方法模式的: 工厂方法模式是类的创建模式,又叫做虚拟构造子(Virtual Constructor)模式或者多态性工厂(Polymor ...

  9. TsinsenA1489 抽奖 【期望】

    题目分析: 问题可以转化成将m个球放进n个盒子里,每个盒子的贡献为盒子中球数的平方. 第一问考虑增量. 对于一个原本有$x$个球的盒子,新加一个球的贡献是$2x+1$.期望条件下仍然满足. 第$i$个 ...

  10. 【BZOJ2721】樱花(数论)

    [BZOJ2721]樱花(数论) 题面 BZOJ 题解 先化简一下式子,得到:\(\displaystyle n!(x+y)=xy\),不难从这个式子中得到\(x,y\gt n!\). 然后通过\(x ...