题目要求很简单,就是给你两个已经排好序的数组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. 甲骨文推出MySQL Fabric,简化MySQL的高可用性与可扩展性

    北京,2014年5月28日——为了满足当下对Web及云应用需求,甲骨文宣布推出MySQL Fabric.MySQL Fabric是一款可简化管理MySQL数据库群的整合式系统.该产品通过故障检测和故障 ...

  2. php面向对象(一) 初窥

    初窥php面向对象 1.类:类就是属性和方法的集合 是一个抽象的概念比如生活中的“人”,"汽车"2.对象:对象是具体的事物 比如一叫“小强”的人 一辆叫“奥迪a7”的汽车3.类和对 ...

  3. 配置Tomcat 中文字符集问题

    找到Tomcat安装路径下的conf文件夹下的server.xml配置文件,修改配置Tomcat端口的标签"Connector",添加URIEncoding属性,代码如下: < ...

  4. How can I work smarter, not just harder? Ask it forever

    How can I  work smarter, not just harder? 记住,永远要问自己这个问题.当你发现在做一件事情时,总是那么的繁琐无味,那么一定是出了什么问题. 如果一味地强调更加 ...

  5. ASP.NET MVC路由配置

    一.命名参数规范+匿名对象 routes.MapRoute(name: "Default", url: "{controller}/{action}/{id}" ...

  6. asp.net,CSS设置<TableListView>的title居左,居左,居上

    居左 DIV.TableTitleStyle TABLE.grid TH { text-align:left; } 引用 <div class="TableTitleStyle&quo ...

  7. HDU 1950 Bridging signals

    那么一大篇的题目描述还真是吓人. 仔细一读其实就是一个LIS,还无任何变形. 刚刚学会了个二分优化的DP,1A无压力. //#define LOCAL #include <iostream> ...

  8. tarjan总结

    先说一下割点跟割边吧. 割桥就是如果一个连通图里删除这条边之后,这个图会变成两个连通图,那么这条边就称之为割桥. 这是割桥的代码,里面呆着lca求法. 割点和割桥的就是用一个时间戳和回到祖先确定. 用 ...

  9. POJ 1837 Balance 【DP】

    题意:给出一个天平,给出c个钩子,及c个钩子的位置pos[i],给出g个砝码,g个砝码的质量w[i],问当挂上所有的砝码的时候,使得天平平衡的方案数, 用dp[i][j]表示挂了前i个砝码时,平衡点为 ...

  10. java程序员修炼之道

    今天在论坛里看到了一位工作10年的java大牛总结的java程序员修炼之道,看完后给出的评价是:字字玑珠,深入人心,猛回头,自己一无是处··· 大牛告诉我们应该好好学习与修炼以下知识与技能 Java语 ...