# @link http://www.cnblogs.com/zuoyuan/p/3759682.html

class Solution(object):
def findMedianSortedArrays(self, nums1, nums2):
"""
:type nums1: List[int]
:type nums2: List[int]
:rtype: float
"""
len1 = len( nums1 )
len2 = len( nums2 )
if( len1 + len2 ) % 2 == 1:
return self.getKth(nums1,nums2,(len1+len2)/2 + 1)
else:
return ( self.getKth(nums1,nums2,(len1+len2)/2) + self.getKth(nums1,nums2,(len1+len2)/2+1) ) * 0.5 def getKth(self,nums1,nums2,k):
len1 = len(nums1)
len2 = len(nums2)
if len1 > len2:
return self.getKth(nums2,nums1,k)
if len1 == 0:
return nums2[k-1]
if k == 1:
return min( nums1[0],nums2[0])
pa = min(k/2,len1)
pb = k - pa
if nums1[pa-1] < nums2[pb-1]:
return self.getKth(nums1[pa:],nums2,pb)
else:
return self.getKth(nums1,nums2[pb:],pa)

leetcode find median sorted arrays python的更多相关文章

  1. [leetcode]Median of Two Sorted Arrays @ Python

    原题地址:https://oj.leetcode.com/problems/median-of-two-sorted-arrays/ 题意:There are two sorted arrays A ...

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

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

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

  4. leetcode Merge K sorted Lists python

    # Definition for singly-linked list. # class ListNode(object): # def __init__(self, x): # self.val = ...

  5. leetcode Merge Two Sorted Lists python

    # Definition for singly-linked list. # class ListNode(object): # def __init__(self, x): # self.val = ...

  6. [LeetCode][Python]Median of Two Sorted Arrays

    # -*- coding: utf8 -*-'''https://oj.leetcode.com/problems/median-of-two-sorted-arrays/ There are two ...

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

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

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

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

随机推荐

  1. JAVA装饰器模式

    Java程序员们应该对java.io对不会陌生,因为java.io包采用了装饰器模式. 一.定义: Decorator装饰器,顾名思义,就是动态地给一个对象添加一些额外的职责,就好比为房子进行装修一样 ...

  2. 使用ionic与cordova(phonegap)进行轻量级app开发前的环境配置与打包安卓apk过程记录

     前言 有人说:"如果你恨一个人,就让ta去接触cordova(phonegap)",这是因为这里面的水很深,坑很多,真让人不是一般地发狂.或许有幸运的人儿基本顺顺利利就配置完环境 ...

  3. html系列教程--ol ul li

    <li> 标签:配合ol,ul实现有序,无序列表以及导航实现. demo: <ol>    <li>Coffee</li>    <li>T ...

  4. 十全大补DBA学习资源

    学习oracle已经有1年多了,从开始的菜鸟到现在的DBA,一路走来~迷茫过.兴奋过.但我仍然会在DBA的道路上走下去!oracle要学的有很多,会遇到很多难题,网上有很多学习oracle好的学习资料 ...

  5. getopt()函数

    在讨论这个函数之前我们先理解两个概念:选项及其参数 gcc -o program program.c 在上述命令中 -o 就是其选项 program就是参数. getopt(): 头文件: #incl ...

  6. C#用网易邮箱发送邮件(同步异步)

    SmtpClient smtpServer = new SmtpClient("smtp.163.com"); smtpServer.Port = ; smtpServer.Cre ...

  7. Deep Clone 常用方式总结

    Deep Clone Example 总结 Deep Clone 一般有如下几种实现方式: 纯手工每个类实现赋值 (ps: 不做介绍,一般都不想这么玩) 序列化和反序列化 纯反射 emit 或 Exp ...

  8. api (三)文本字符输出 (转)

    在使用Win32编程时,我们常常要输出文本到窗口上,Windows所有的文本字符或者图形输出都是通过图形设备接口(GDI)进行的,Windows的三大核心组件之一的GDI32.dll封装了所有的文本和 ...

  9. swift和objc之對比

    http://mobidev.biz/blog/swift_how_we_code_your_ios_apps_twice_faster

  10. 软件测试学习日志————round 1 some questions of two small programs

    Below are four faulty programs. Each includes a test case that results in failure. Answer the follow ...