原题地址:https://oj.leetcode.com/problems/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 arrays. The overall run time complexity should be O(log (m+n)).

解题思路:这道题要求两个已经排好序的数列的中位数。中位数的定义:如果数列有偶数个数,那么中位数为中间两个数的平均值;如果数列有奇数个数,那么中位数为中间的那个数。比如{1,2,3,4,5}的中位数为3。{1,2,3,4,5,6}的中位数为(3+4)/ 2 = 3.5。那么这题最直接的思路就是将两个数列合并在一起,然后排序,然后找到中位数就行了。可是这样最快也要O((m+n)log(m+n))的时间复杂度,而题目要求O(log(m+n))的时间复杂度。这道题其实考察的是二分查找,是《算法导论》的一道课后习题,难度还是比较大的。

       首先我们来看如何找到两个数列的第k小个数,即程序中getKth(A, B , k)函数的实现。用一个例子来说明这个问题:A = {1,3,5,7};B = {2,4,6,8,9,10};如果要求第7个小的数,A数列的元素个数为4,B数列的元素个数为6;k/2 = 7/2 = 3,而A中的第3个数A[2]=5;B中的第3个数B[2]=6;而A[2]<B[2];则A[0],A[1],A[2]中必然不可能有第7个小的数。因为A[2]<B[2],所以比A[2]小的数最多可能为A[0], A[1], B[0], B[1]这四个数,也就是说A[2]最多可能是第5个大的数,由于我们要求的是getKth(A, B, 7);现在就变成了求getKth(A', B, 4);即A' = {7};B不变,求这两个数列的第4个小的数,因为A[0],A[1],A[2]中没有解,所以我们直接删掉它们就可以了。这个可以使用递归来实现。

代码:

class Solution:
# @return a float
# @line20 must multiply 0.5 for return a float else it will return an int
def getKth(self, A, B, k):
lenA = len(A); lenB = len(B)
if lenA > lenB: return self.getKth(B, A, k)
if lenA == 0: return B[k - 1]
if k == 1: return min(A[0], B[0])
pa = min(k/2, lenA); pb = k - pa
if A[pa - 1] <= B[pb - 1]:
return self.getKth(A[pa:], B, pb)
else:
return self.getKth(A, B[pb:], pa) def findMedianSortedArrays(self, A, B):
lenA = len(A); lenB = len(B)
if (lenA + lenB) % 2 == 1:
return self.getKth(A, B, (lenA + lenB)/2 + 1)
else:
return (self.getKth(A, B, (lenA + lenB)/2) + self.getKth(A, B, (lenA + lenB)/2 + 1)) * 0.5

[leetcode]Median of Two Sorted Arrays @ Python的更多相关文章

  1. LeetCode: Median of Two Sorted Arrays 解题报告

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

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

  3. Leetcode4:Median of Two Sorted Arrays@Python

    There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...

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

  5. LeetCode—— Median of Two Sorted Arrays

    Description: There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the medi ...

  6. Leetcode: Median of Two Sorted Arrays. java.

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

  7. C++ Leetcode Median of Two Sorted Arrays

    坚持每天刷一道题的小可爱还没有疯,依旧很可爱! 题目:There are two sorted arrays nums1 and nums2 of size m and n respectively. ...

  8. LeetCode——Median of Two Sorted Arrays

    Question There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median o ...

  9. 力扣 -- 寻找两个有序数组的中位数 Median of Two Sorted Arrays python实现

    题目描述: 中文: 给定两个大小为 m 和 n 的有序数组 nums1 和 nums2. 请你找出这两个有序数组的中位数,并且要求算法的时间复杂度为 O(log(m + n)). 你可以假设 nums ...

随机推荐

  1. Nmap扫描教程之网络基础服务DHCP服务类

    Nmap扫描教程之网络基础服务DHCP服务类 Nmap网络基础服务 网络基础服务是网络正常工作的基石,常见的网络基础服务包括DHCP服务和DNS服务.其中,DHCP服务用来为计算机动态分配IP地址:D ...

  2. 喵哈哈村的魔法考试 Round #4 (Div.2) 题解

    有任何疑问,可以加我QQ:475517977进行讨论. A 喵哈哈村的嘟嘟熊魔法(1) 题解 这道题我们只要倒着来做就可以了,因为交换杯子是可逆的,我们倒着去模拟一遍就好了. 有个函数叫做swap(a ...

  3. 移动端web,tap与click事件

    一.tap与click的区别 两者都会在点击时系统自动触发,但是在手机WEB端,click会有 200~300 ms.延迟来自判断双击和长按,因为只有默认等待时间结束以确定没有后续动作发生时,才会触发 ...

  4. 基于ngx_lua的动态服务路由方案

    基于ngx_lua的动态服务路由方案 http://geek.csdn.net/news/detail/131497

  5. Revit API改变风管及管件尺寸

    start , , )) < , , -)) < , dHeight = ;             ConnectorSetIterator csi = fi.MEPModel.Conn ...

  6. 怎样正确的使用Cookie的Path详细解析

    原文地址:http://java-zone.org/1052.html cookie 有路径--path,表示哪些路径下的文件有权限读取该 cookie. path 应该以 “/” 结尾,同名 coo ...

  7. EBS查询默认应用用户,比如是否需要锁定、修改这些用户

    /* Formatted on 2018/3/15 13:05:39 (QP5 v5.256.13226.35538) */ --查询默认应用用户,比如是否需要锁定.修改这些用户 SELECT ROW ...

  8. Cocos2d-x开源、跨平台的游戏引擎

    from://http://blog.linguofeng.com/pages/language/c/Cocos2dx.html Cocos2d-x 开源.跨平台的游戏引擎 一.下载 http://c ...

  9. dotnetty 心跳

    IdleStateHandler 需要提供三个参数: readerIdleTimeSeconds, 读超时. 即当在指定的事件间隔内没有从 Channel 读取到数据时, 会触发一个 READER_I ...

  10. StringBuffer与StringBuilder差别

    从JDK源代码能够看出,StringBuffer和StringBuilder都是继承自AbstractStringBuilder,事实上这两个类的功能实现都是在AbstractStringBuilde ...