60.Median of Two Sorted Arrays(两个排序数组的中位数)
Level:
Hard
题目描述:
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
思路分析:
该方法的核心是将原问题转变成一个寻找第k小数的问题(假设两个原序列升序排列),这样中位数实际上是第(m+n)/2小的数。所以只要解决了第k小数的问题,原问题也得以解决。
首先假设数组A和B的元素个数都大于k/2,我们比较A[k/2-1]和B[k/2-1]两个元素,这两个元素分别表示A的第k/2小的元素和B的第k/2小的元素。这两个元素比较共有三种情况:>、<和=。如果A[k/2-1]<B[k/2-1],这表示A[0]到A[k/2-1]的元素都在A和B合并之后的前k小的元素中。换句话说,A[k/2-1]不可能大于两数组合并之后的第k小值,所以我们可以将其抛弃。
当A[k/2-1]>B[k/2-1]时存在类似的结论。
当A[k/2-1]=B[k/2-1]时,我们已经找到了第k小的数,也即这个相等的元素,我们将其记为m。由于在A和B中分别有k/2-1个元素小于m,所以m即是第k小的数。(这里可能有人会有疑问,如果k为奇数,则m不是中位数。这里是进行了理想化考虑,在实际代码中略有不同,是先求k/2,然后利用k-k/2获得另一个数。)
通过上面的分析,我们即可以采用递归的方式实现寻找第k小的数。此外我们还需要考虑几个边界条件:
如果A或者B为空,则直接返回B[k-1]或者A[k-1];
如果k为1,我们只需要返回A[0]和B[0]中的较小值;
如果A[k/2-1]=B[k/2-1],返回其中一个;
代码:
public class Solution{
public double findMedianSortedArrays(int []A,int []B){
int m=A.length;
int n=B.length;
int l=(m+n+1)/2;
int r=(m+n+2)/2; //两个数组的长度和可能是奇数也可能是偶数
return(getkth(A,0,B,0,l)+getkth(A,0,B,0,r))/2;
}
public double getkth(int []A,int astart,int[]B,int bstart,int k){
if(astart>A.length-1)
return B[bstart+k-1];
if(bstart>B.length-1)
return A[astart+k-1];
if(k==1)
return Math.min(A[astart],B[bstart]);
int amid=Integer.MAX_VALUE;
int bmid=Integer.MAX_VALUE;
if(astart+k/2-1<A.length)
amid=A[astart+k/2-1];
if(bstart+k/2-1<B.length)
bmid=B[bstart+k/2-1];
if(amid<bmid)
return getkth(A,astart+k/2,B,bstart,k-k/2);
else
return getkth(A,astart,B,bstart+k/2,k-k/2);
}
}
60.Median of Two Sorted Arrays(两个排序数组的中位数)的更多相关文章
- 2.Median of Two Sorted Arrays (两个排序数组的中位数)
要求:Median of Two Sorted Arrays (求两个排序数组的中位数) 分析:1. 两个数组含有的数字总数为偶数或奇数两种情况.2. 有数组可能为空. 解决方法: 1.排序法 时间复 ...
- Leetcode4.Median of Two Sorted Arrays两个排序数组的中位数
给定两个大小为 m 和 n 的有序数组 nums1 和 nums2 . 请找出这两个有序数组的中位数.要求算法的时间复杂度为 O(log (m+n)) . 你可以假设 nums1 和 nums2 不同 ...
- [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] 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 ...
- [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 ...
- 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 ...
- 【medium】4. Median of Two Sorted Arrays 两个有序数组中第k小的数
There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...
- 【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 ...
- 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 ...
- Median of Two Sorted 求两个有序数组的中位数
中位数是把一个数的集合划分为两部分,每部分包含的数字个数相同,并且一个集合中的元素均大于另一个集合中的元素. 因此,我们考虑在一个任意的位置,将数组A划分成两部分.i表示划分数组A的位置,如果数组A包 ...
随机推荐
- PCA算法和实例
PCA算法 算法步骤: 假设有m条n维数据. 1. 将原始数据按列组成n行m列矩阵X 2. 将X的每一行(代表一个属性字段)进行零均值化,即减去这一行的均值 3. 求出协方差矩阵C=1/mXXT 4. ...
- [转载]Linux上使用ssl进行端口转发
原文地址:Linux上使用ssl进行端口转发 作者:呼延十 背景介绍 作为一个后端程序员,经常要和别人联调接口,每当这时,总是被公司的各种,dev,qa,pre,prod环境搞得头疼,,,我真的只是想 ...
- linux系统使用和优化的原则
- pwn的一些技巧与总结
原文地址:https://github.com/Naetw/CTF-pwn-tips 目录 溢出 在gdb中寻找字符串 二进制服务 找到libc中特定函数的偏移地址 Find '/bin/sh' or ...
- CS184.1X 计算机图形学导论(第五讲)
一.观察:正交投影 1.特性:保持平行线在投影后仍然是平行的 2.一个长方体,对处在只有深度不同的位置上的同一物体来说,它的大小不会改变. 3.透视投影:平行线在远处会相交(例如铁轨) 4.glOrt ...
- java两个引用指向同一个对象
- MySQL执行外部sql脚本文件命令报错:unknown command '\'
由于编码不一致导致的 虽然大部分导出是没有问题的 但是数据表中存储包含一些脚本(富文本内容)会出现该问题,强制指定编码即可解决. mysql导入时指定编码: mysql -u root -p --de ...
- shell脚本特殊变量与变量子串相关知识
一.shell脚本特殊变量 1.shell中常用特殊位置变量说明: $0 获取当前执行的shell脚本的文件名,如果执行脚本包含了路径,那么就包含了脚本路径 $n 获取当前执行的shell脚本的第n个 ...
- CNN笔记:通俗理解卷积神经网络
CNN笔记:通俗理解卷积神经网络 2016年07月02日 22:14:50 v_JULY_v 阅读数 250368更多 分类专栏: 30.Machine L & Deep Learning 机 ...
- spring boot jar的支持