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(两个排序数组的中位数)的更多相关文章

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

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

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

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

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

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

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

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

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

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

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

  10. Median of Two Sorted 求两个有序数组的中位数

    中位数是把一个数的集合划分为两部分,每部分包含的数字个数相同,并且一个集合中的元素均大于另一个集合中的元素. 因此,我们考虑在一个任意的位置,将数组A划分成两部分.i表示划分数组A的位置,如果数组A包 ...

随机推荐

  1. Nginx学习总结:proxy与rewrite模块(三)

    斜体下划线,表示建议采用默认配置,无需显式的配置 一.ngx_http_upstream_module 此模块中可配置的指令并不是很多.nginx的负载均衡算法包括: 1)round-robin:轮询 ...

  2. egrep 或 多个连续字符测数字

    .TXT 4-6是ABC 或者 4-6是 0-9

  3. OpenStack虚拟机网络问题

    当发现你的OpenStack虚拟机网络有问题,不妨先试一下这16个步骤   1. Security Group全部打开,这是最基本的,但是很多人容易忘记 其实遇到过无数这种场景了,Debug了半天网络 ...

  4. BZOJ2756 [SCOI2012]奇怪的游戏 最大流

    好久没有写博客了.不过这个博客也没有多少人看 最近在写网络流,为了加深理解,来写一两篇题解. 对整个棋盘进行黑白染色以后可以发现,一次操作就是让二分图的两个点的值分别 \(+1\). 这样,我们就可以 ...

  5. 解决 linux 下安装 node 报: command not found

    注意:有时安装成功后,需要关闭xshell,重新启动.nvm才会生效. 1. 在 linux 下安装 node 提示 -bash:  node: command not found. 2. 这种情况可 ...

  6. netty-Selector

    上图中加入一句: socketChannel.configureBlocking(false);//设置为非阻塞的 keyIterator.clear(); 每连接一个SocketChannel 都会 ...

  7. Vue-Router原理

    Hash 与 History 路由原理 实现路由 /** * 1.前端路由与后端路由的区别 后端路由: 输入url => 请求发送到服务器 => 服务器解析请求路径 => 拿到对应页 ...

  8. Oracle or Question Solve(一)

    Oracle查看版本命令:select * from v$version; ORACLE_BASE和ORACLE_HOME路径查看su - oracleecho $ORACLE_BASEecho $O ...

  9. 使用xorm将结构体转为sql文件

    操作步骤 (1)定义结构体 type User struct { Id int //表id Name string //姓名 ...}12345(2)编写代码,执行自动增量同步(mysql为例) im ...

  10. linux 组合命令

    统计home目录下面有多少文件 ls -l  /home|grep '^-'|wc -l