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包 ...
随机推荐
- unity ui坐标系转换
世界坐标: transform.position获取的是世界坐标 屏幕坐标: 单位像素 屏幕左下角(0,0)右上角(Screen.width,Screen.height) Screen.width = ...
- Java-java.lang.NoClassDefFoundError:brave.Span.Kind
今天在升级某个框架时,遇到如标题描述的问题.这个问题应该说还是比较明显的,首先去搜了一下NoClassDefFoundError的问题,参考这篇博客:https://www.cnblogs.com/x ...
- 微信小程序(10)--开发者工具更新以后wxss编译错误
更新最新版微信开发者工具后,出现下面报错: 解决办法: 1.在控制台输入openVendor() ,确定Enter: 2.清除里面的wcc.exe wcsc.exe : 3.重启开发者工具
- 关于Insufficient space for shared memory file解决办法
发现这个目录使用率100%,但是这个只是逻辑卷,具体是由于/tmp目录下,日志文件太多,导致空间被占满了.
- Go 使用 append 向切片增加元素
1.// 创建一个整型切片 // 其长度和容量都是 5 个元素 slice := []int{10, 20, 30, 40, 50} // 创建一个新切片 // 其长度为 2 个元素,容量为 4 个元 ...
- 【串线篇】spring boot配置文件大全【上】
一.配置文件 SpringBoot使用一个全局的配置文件,配置文件名是固定的: • application.properties • application.yml 配置文件的作用:修改SpringB ...
- js image转canvas不显示
今天在项目开发中遇到了image转canvas不显示的问题,最后翻了不少资料才发现问题出现在图片加载上 如果你的代码是这样的,那么不显示的原因就是img没有加载完成 function convertI ...
- 前端-个人网页开发最常用的插件Superslide 与 swiper
给初入前端的同学,安利2个轮播图插件 SuperSlide 与 Swiper ! SuperSlide PC端与移动端 的轮播图特效都是分开写2个js插件,而Swiper 基本一个插件可以写PC端与移 ...
- [原创] Delphi InputBox、InputQuery函数
Delphi InputBox.InputQuery函数 两个函数都是弹框提示输入信息 function InputQuery(const ACaption, APrompt: string; var ...
- Tomcat 中get请求中含有中文字符时乱码的处理
Tomcat 中get请求中含有中文字符时乱码的处理