There are two sorted arrays A and B 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)).


题解:注意这里对median的理解,如果A,B合并后的序列有奇数个元素,那么中间元素就是下标为(a.length+b.length)/2的元素;而如果合并后的序列有偶数个元素,那么median是下标为(a.length+b.length)/2和(a.length+b.length)/2-1两个元素的平均数。

我们实现一个二分在有序两个数组中找第k小的数的函数,然后在主函数中,根据合并后数组元素个数的奇偶性调用这个函数。

首先来看这个在两个有序数组中找第k小的数的函数 private double findKth(int a[],int b[],int k,int a_start,int b_start){ 。它的原理如下图所示:

即:

if(a_mid < b_mid)
return findKth(a, b, k-k/2, a_start+k/2, b_start);
else
return findKth(a, b, k-k/2, a_start, b_start+k/2);

再来看求解函数 public double findMedianSortedArrays(int A[], int B[]) ,当A和B合并后元素个数为奇数的时候,我们直接调用 findKth(A, B, (B.length+A.length)/2+1, 0, 0) 找到第(A.length+B.length)/2的数就是中位数了。而当A和B的合并后元素个数为偶数的时候,我们要调用两次findKth分别找到第(A.length+B.length)/2和第(A.length+B.length)/2-1的数,然后求它们的平均数,即 (findKth(A, B, (A.length+B.length)/2, 0, 0) + findKth(A, B, (A.length+B.length)/2+1, 0, 0)) / 2.0 。

代码如下:

 public class Solution {
private double findKth(int a[],int b[],int k,int a_start,int b_start){
//if a is empty
if(a_start >= a.length)
return b[b_start+k-1];
if(b_start >= b.length)
return a[a_start+k-1]; if(k == 1)
return Math.min(a[a_start], b[b_start]); int a_mid = a_start + k/2 -1 < a.length?a[a_start+k/2-1]:Integer.MAX_VALUE;
int b_mid = b_start + k/2 -1 < b.length?b[b_start+k/2-1]:Integer.MAX_VALUE; if(a_mid < b_mid){
return findKth(a, b, k-k/2, a_start+k/2, b_start);
}
else
return findKth(a, b, k-k/2, a_start, b_start+k/2); }
public double findMedianSortedArrays(int A[], int B[]) {
int len = A.length + B.length;
if(len % 2 == 0)
return (findKth(A, B, len/2, 0, 0) + findKth(A, B, len/2+1, 0, 0)) / 2.0;
else
return findKth(A, B, len/2+1, 0, 0);
}
}

【leetcode刷题笔记】Median of Two Sorted Arrays的更多相关文章

  1. Kotlin实现LeetCode算法题之Median of Two Sorted Arrays

    题目Median of Two Sorted Arrays(难度Hard) 方案1,数组合并&排序调用Java方法 import java.util.* class Solution { fu ...

  2. 刷题4. Median of Two Sorted Arrays

    一.题目 Median of Two Sorted Arrays,具体请自行搜索. 这个题目,我看了一下,经过一番思考,我觉得实现起来不是很复杂. 但要做到bug free也不难,最大的问题是性能问题 ...

  3. 【leetcode刷题笔记】Merge k Sorted Lists

    Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 题 ...

  4. (python)leetcode刷题笔记04 Median of Two Sorted Arrays

    4. Median of Two Sorted Arrays There are two sorted arrays nums1 and nums2 of size m and n respectiv ...

  5. leetcode第四题:Median of Two Sorted Arrays (java)

    Median of Two Sorted Arrays There are two sorted arrays A and B of size m and n respectively. Find t ...

  6. LeetCode刷题笔记和想法(C++)

    主要用于记录在LeetCode刷题的过程中学习到的一些思想和自己的想法,希望通过leetcode提升自己的编程素养 :p 高效leetcode刷题小诀窍(这只是目前对我自己而言的小方法,之后会根据自己 ...

  7. 18.9.10 LeetCode刷题笔记

    本人算法还是比较菜的,因此大部分在刷基础题,高手勿喷 选择Python进行刷题,因为坑少,所以不太想用CPP: 1.买股票的最佳时期2 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. ...

  8. LeetCode刷题笔记 - 12. 整数转罗马数字

    学好算法很重要,然后要学好算法,大量的练习是必不可少的,LeetCode是我经常去的一个刷题网站,上面的题目非常详细,各个标签的题目都有,可以整体练习,本公众号后续会带大家做一做上面的算法题. 官方链 ...

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

随机推荐

  1. 一步步教你搭建TinyOS2.1.2开发环境

    (本教程使用的是VirtualBOX +ubuntu14.04+tinyos2.1.2) note:看了非常多的tinyos的安装教程.区别不是非常大,无非就是安装编译器配置环境等.尽管简单,但还是有 ...

  2. hdu 4005(边双连通)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4005 思路:首先考虑边双连通分量,如果我们将双连通分量中的边删除,显然我们无法得到非连通图,因此要缩点 ...

  3. Android错误——基础篇

    1. Android工程在真机上运行调试: 花了二个小时的时间来把App热部署到小米机上,简直让我寒透了心, 原本是按照网上提供的步骤一步步的做着,没想到小米神机居然出的是什么内测小米助手,两个窗口来 ...

  4. [JAVA &#183; 0基础]:19.容器类

    概述 容器 就是存放数据的一个集合 (java提供了一个接口专门去约束我们容器的实现类) watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L ...

  5. iOS-ASIHTTPRequest框架学习

    本文转载至 http://www.cnblogs.com/A-Long-Way-Chris/p/3539679.html 前段时间在公司的产品中支持了够快网盘,用于云盘存储. 在这个过程中,学习到了很 ...

  6. Android开发:《Gradle Recipes for Android》阅读笔记(翻译)3.3——整合resource文件

    问题: 想要在product的flavor里面改变图片,文字或者其它资源. 解决方案: 在flavor里面增加合适的资源目录,并且改变他们包含的值. 讨论: 考虑下3.2章的“hello world ...

  7. Kettle 7启动 Spoon.bat 时报错“A Java Exception has occurred.”的解决方法

    最近在研究Kettle 时出现启动时报错“A Java Exception has occurred.”的问题.刚开始没搞明白是什么原因,后来发现是jdk版本的问题.出现这个错误原因是 Kettle ...

  8. fiddler抓包工具使用图文教程

    一.软件简介: 一款免费且功能强大的数据包抓取软件.它通过代理的方式获取程序http通讯的数据,可以用其检测网页和服务器的交互情况,能够记录所有客户端和服务器间的http请求,支持监视.设置断点.甚至 ...

  9. 常见cout格式输出

    cout.setf(ios::fixed);//设置格式 cout.unsetf(ios::fixed);//取消格式 cout.setf(ios::scientific);//科学记数法 cout. ...

  10. 计算属性 vs 侦听属性 当需要在数据变化时执行异步或开销较大的操作时,这个方式是最有用的

    https://cn.vuejs.org/v2/guide/computed.html#基础例子 计算属性 vs 侦听属性 Vue 提供了一种更通用的方式来观察和响应 Vue 实例上的数据变动:侦听属 ...