题目要求很简单,就是给你两个已经排好序的数组nums1(长度为m)和nums2(长度为n),找出他们的中间值。返回值类型double是因为如果数字个数是偶数个,就要返回中间两个数的平均值。这题最简单的方法就是通过两个指针分别从nums1和nums2的头一直移动,直到走到nums1和nums2的中值位置为止。整个过程耗时大概是O((m+n) / 2)。但是题目附带需要用O(log(m+n))的时间完成,所以上述方法就不能用了。能用到O(log(m+n))的方法在leetcode的算法题里面没有几个,二分法是最常见的一种。其实一般来说,array,sorted,寻找某个数,看到这些关键词,就可以考虑(不是一定)使用二分法来做。问题是,这一题怎么使用二分法?

  其实这一个问题可以延伸至在两个数组之间寻找第kth个元素,只是现在k = (m+n) / 2。为了方便理解,我举个例子,假设有两个数组,分别是:

nums1 = [1,4,5,6,8,10]

nums2 = [1,2,4,7,9,11]

  因为我要找到第k个数,所以我把nums1从k/2处划分两部分,把nums2也从k/2处划分两部分,此题中,k/2 = (12 / 2) / 2 = 3,故index 应该是k/2 - 1处:

1 4 5 | 6 8 10

1 2 4 | 7 9 11

  这时候,比较nums1和nums2在index处的大小,发现nums2[index] < nums1[index],这个结果我们可以保证,第k个数一定不在1,2,4里面(为什么?请读者自己证明),所以我们可以大胆从nums2里面把1,2,4给删除。这样,就把在两个数组间寻找第k个元素转变为寻找第(k - (index+1))个元素,继续进入递归或者迭代,直到找到那个第k个元素为止。那什么时候算找到呢?有三种情况:

  1. 当其中一个数组被删光(假设为nums1),则返回的就是nums2[k'-1](这个k'是迭代多次后的k')。
  2. 当k=1的时候,那么,只需要找nums1[0]和nums2[0]中的较小者就可以。
  3. 当nums1[k/2 - 1] = nums2[k/2 - 1]的时候,则这个数就是所要寻找的第k个数。

  当然,如果nums1的长度小于k/2怎么办?那么就用nums1[m-1]和nums2[k - m - 1]比较,比较后的处理也是一样的。代码如下:

  

 //总体思路:把寻找两个sorted数组的中值转变为寻找两个sorted数组的第k大元素。
public class Solution {
public double findMedianSortedArrays(int[] nums1, int[] nums2) {
int total = nums1.length + nums2.length;
//元素个数为基数的时候
if (total % 2 != 0) {
return (double)findKth(nums1,nums2,total / 2 + 1);
}
//元素个数为偶数的时候
else {
return (findKth(nums1,nums2,total/2) +
findKth(nums1,nums2,total/2 + 1)) / 2.0;
}
} private int findKth(int[] nums1, int[] nums2, int k) {
//默认nums1的长度小于num2的长度,省去很多不必要的判断。
if (nums1.length > nums2.length) return findKth(nums2,nums1,k);
if (nums1.length == 0) return nums2[k-1];
if (k == 1) return Math.min(nums1[0],nums2[0]); //把K分成两部分。如果nums1.length < k/2,则取k/2个数,否则取nums1.length个数;
int index1 = Math.min(k/2,nums1.length);
//nums2里的前k-index1个数。
int index2 = k - index1;
//-1是因为index从0开始,第index1个数就是index1 - 1;
//当num1 < num2时,去除掉nums1里包括index1-1之前的所有数(index1个)。
if (nums1[index1 - 1] < nums2[index2 - 1]) {
int[] temp1 = new int[nums1.length - index1];
System.arraycopy(nums1,index1,temp1,0,nums1.length - index1);
//更新k值,进入下个递归。
return findKth(temp1,nums2,k-index1);
}
//情况与上面相反。
else if (nums1[index1 - 1] > nums2[index2 - 1]) {
int[] temp2 = new int[nums2.length - index2];
System.arraycopy(nums2,index2,temp2,0,nums2.length - index2);
return findKth(nums1,temp2,k - index2);
}
//两者相同,说明找到了k值。
else {
return nums1[index1 - 1];
}
}
}

  当然,代码还可以继续优化,例如加两个变量,可以直接在nums1和nums2上递归而不需要重新建一个数组,这样就能节省不少空间和时间,但是总体的思路是一样的。

LeetCode(4) - Median of Two Sorted Arrays的更多相关文章

  1. 【LeetCode】4. Median of Two Sorted Arrays (2 solutions)

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

  2. 《LeetBook》leetcode题解(4): Median of Two Sorted Arrays[H]——两个有序数组中值问题

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

  3. leetcode.C.4. Median of Two Sorted Arrays

    4. Median of Two Sorted Arrays 这应该是最简单最慢的方法了,因为本身为有序,所以比较后排序再得到中位数. double findMedianSortedArrays(in ...

  4. leetcode第二题--Median of Two Sorted Arrays

    Problem:There are two sorted arrays A and B of size m and n respectively. Find the median of the two ...

  5. 【一天一道LeetCode】#4 Median of Two Sorted Arrays

    一天一道LeetCode (一)题目 There are two sorted arrays nums1 and nums2 of size m and n respectively. Find th ...

  6. 【LeetCode OJ】Median of Two Sorted Arrays

    题目链接:https://leetcode.com/problems/median-of-two-sorted-arrays/ 题目:There are two sorted arrays nums1 ...

  7. Leetcode Array 4 Median of Two Sorted Arrays

    做leetcode题目的第二天,我是按照分类来做的,做的第一类是Array类,碰见的第二道题目,也就是今天做的这个,题目难度为hard.题目不难理解,但是要求到了时间复杂度,就需要好好考虑使用一下算法 ...

  8. 【LeetCode】4. Median of Two Sorted Arrays 寻找两个正序数组的中位数

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:数组,中位数,题解,leetcode, 力扣,python ...

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

随机推荐

  1. 寻找最小的k个数

    1. 能想到的最直接的办法,就是对数组进行排序,最好的排序算法的时间复杂性为O(n*logn),这一个方法请参照各种排序算法. 2. 另外申请一个k空间数组,依次更改里面的最大值,每做一次最多要扫描一 ...

  2. vi 每日练习

    vi 1.    4 空格 2.    ctrl + b, ctrl + f 3.     0 $ 4.    1G gg G 5.    10G 6.    10 回车 7.    / ? 8.   ...

  3. 用VS2005开发WinCE程序调试图文教程

    一.WinCE 模拟器通过ActiveSync 6.1(即Windows Mobile设备中心)连接P 1.启动WinCE模拟器 命令行:  start .\DeviceEmulator.exe WI ...

  4. LA 3027 Corporative Network

    这题感觉和 POJ 1988 Cube Stacking 很像,在路径压缩的同时递归出来的时候跟新distant数组 我发现我一直WA的原因是,命令结束是以字母o结束的,而不是数字0!! //#def ...

  5. HDU 2126 (背包方法数) Buy the souvenirs

    DP还有很长很长一段路要走.. 题意:给出n纪念品的价格和钱数m,问最多能买多少件纪念品和买这些数量的纪念品的方案数. 首先,求能买最多的纪念品的数量,用贪心法可以解决.将价钱排序,然后从最便宜的开始 ...

  6. HDU 1160 FatMouse's Speed

    半个下午,总算A过去了 毕竟水题 好歹是自己独立思考,debug,然后2A过的 我为人人的dp算法 题意: 为了支持你的观点,你需要从给的数据中找出尽量多的数据,说明老鼠越重速度越慢这一论点 本着“指 ...

  7. 如何使用USB安装XenServer 6.x

    在XenServer 5.6以前我们能够很容易的通过一些工具,直接制作USB安装介质,然后快速安装XenServer,但是我们发现,到XenServer6.0以后,通过工具直接制作的XenServer ...

  8. mysql-备份和还原(普通还原和binlog还原)

    1)备份 mysqldump -uroot -proot share -l -F > /tmp/share.sql 说明:-l 锁表 -F 刷新日志文件(相当于flush logs) 2)还原( ...

  9. (六)6.11 Neurons Networks implements of self-taught learning

    在machine learning领域,更多的数据往往强于更优秀的算法,然而现实中的情况是一般人无法获取大量的已标注数据,这时候可以通过无监督方法获取大量的未标注数据,自学习( self-taught ...

  10. Android手机部分名词浅谈

    1.什么是Radio? Radio是无线通信模块的驱动程序,ROM是系统程序,radio负责网络通信,ROM和Radio可以分开刷,互不影响.如果您的手机刷新了ROM后有通讯方面的问题可以刷新radi ...