# @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中文乱码解决之道(三)—–编码详情:伟大的创想—Unicode编码

    原文出处:http://cmsblogs.com/?p=1458 随着计算机的发展.普及,世界各国为了适应本国的语言和字符都会自己设计一套自己的编码风格,正是由于这种乱,导致存在很多种编码方式,以至于 ...

  2. 动画原理——绘画API

    书籍名称:HTML5-Animation-with-JavaScript 书籍源码:https://github.com/lamberta/html5-animation 1.canvas的conte ...

  3. OC语法10——@protocol协议,

    参考资料:博客 @protocol,协议: OC中protocol的含义和Java中接口的含义是一样的,它们的作用都是为了定义一组方法规范. 实现此协议的类里的方法,必须按照此协议里定义的方法规范来. ...

  4. Duplicate files copied in APK META-INF/LICENSE.txt

    Error:Execution failed for task ':app:packageDebug'. > Duplicate files copied in APK META-INF/LIC ...

  5. 【grunt整合版】学会使用grunt打包前端代码

    grunt 是一套前端自动化工具,一个基于nodeJs的命令行工具,一般用于:① 压缩文件② 合并文件③ 简单语法检查 对于其他用法,我还不太清楚,我们这里简单介绍下grunt的压缩.合并文件,初学, ...

  6. PHP MySql数据库访问

    PHP MySql数据库访问 计应134   凌豪 1.MySql数据库的连接 要操作MySql数据库,首先必须与MySQl数据库建立连接,连接MySQL服务器的语句如下: <?php$link ...

  7. 【Howie玩docker】-Docker常用命令操作

    attach     附加到一个运行的容器上面       --no-stdin=false Do not attach stdin       --sig-proxy=true Proxify al ...

  8. Couldn't read row 0, col -1 from CursorWindow

    java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow.  Make sure the Curso ...

  9. 考察printf函数返回值

    最近偶然间见了这样一道题:  #include<stdio.h> int main() { ; printf("%d\n",printf("%d", ...

  10. impala安装

    http://blog.sina.com.cn/s/blog_8c6d7ff60101e3lh.html ----------------------------------------------- ...