# @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基础学习笔记1

    Dos的基本命令: Dir:列出当前目录的所有文件和文件夹 Md:创建一个目录 Rd:删除目录 Cd:进入指定的目录 Cd..:退回上一级目录 Cd/:退回根目录 Del:删除文件 Exit:退出do ...

  2. c++中冒号(:)和双冒号(::)的用法

    1.冒号(:)用法 (1)表示机构内位域的定义(即该变量占几个bit空间) typedef struct _XXX{ unsigned ; unsigned char c; } ; XXX (2)构造 ...

  3. 在WPF中将某张表中的数据显示到datagrid

    a.在.xaml文件中拖入一个datagrid,然后添加列名,使用Binding="{Binding 数据库中的 列名称}",如下: <DataGrid AutoGenera ...

  4. util 学习

    const I = 3.4893589; console.log(Number.parseInt(I)); console.log(Number.parseFloat(I)); console.log ...

  5. js计时器。

    <html> <head> <title> Nonove js clock 时钟 </title> <script type="text ...

  6. 常用Python第三方库 简介

    如果说强大的标准库奠定了python发展的基石,丰富的第三方库则是python不断发展的保证,随着python的发展一些稳定的第三库被加入到了标准库里面,这里有6000多个第三方库的介绍:点这里或者访 ...

  7. MAC上使用maven打android的包,报错:No Android SDK path could be found. 解决办法

    对android工程运行mvn compile出现如下信息: No Android SDK path could be found. You may configure it in the pom u ...

  8. php多维数组按用户自定义顺序排序uasort()

    多维数组如果需要按照某个字段值进行排序,可以尝试array_multisort().但有一个前提,这个字段值的排序是要一定规律的,如字符升序a-z,或者数字降序等等. 现有多维数组如下: $arr = ...

  9. c++ containers

    顺序容器 array(C++11) vector string deque forward_list(C++11) list 容器适配器 stack queue priority_queue 关联容器 ...

  10. c语言中的制表符\t与空格

    (本文不讨论制表符与空格缩进问题) 编程过程中,我们常常用多个空格或制表符分隔两个字符串,那么这两个在显示效果上有什么区别呢? 比较如下两行代码的输出效果 代码1: printf("1\t1 ...