leetcode find median sorted arrays python
# @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的更多相关文章
- [leetcode]Median of Two Sorted Arrays @ Python
原题地址:https://oj.leetcode.com/problems/median-of-two-sorted-arrays/ 题意:There are two sorted arrays A ...
- 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 ...
- 力扣 -- 寻找两个有序数组的中位数 Median of Two Sorted Arrays python实现
题目描述: 中文: 给定两个大小为 m 和 n 的有序数组 nums1 和 nums2. 请你找出这两个有序数组的中位数,并且要求算法的时间复杂度为 O(log(m + n)). 你可以假设 nums ...
- leetcode Merge K sorted Lists python
# Definition for singly-linked list. # class ListNode(object): # def __init__(self, x): # self.val = ...
- leetcode Merge Two Sorted Lists python
# Definition for singly-linked list. # class ListNode(object): # def __init__(self, x): # self.val = ...
- [LeetCode][Python]Median of Two Sorted Arrays
# -*- coding: utf8 -*-'''https://oj.leetcode.com/problems/median-of-two-sorted-arrays/ There are two ...
- (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 ...
- 【LeetCode】4. Median of Two Sorted Arrays 寻找两个正序数组的中位数
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:数组,中位数,题解,leetcode, 力扣,python ...
- [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 ...
随机推荐
- 略谈cpu架构种类
一直对x86/i386/i686/x86_64这些东西感觉很不清楚,查些资料,解决部分问题,小记一番. Question1:什么是x86? x86或80x86是英特尔Intel首先开发制造的一种微处理 ...
- Repeater的ItemCommand事件和ItemCreated事件,高手请跳过~
捣鼓这几天,我终于比之前更能区别Repeater的ItemCommand事件和ItemCreated事件了 当Repeater的dataSource是sqldataSource的话,要想触发ItemC ...
- div内嵌p,div等块元素出现的问题
div内嵌p,div等块元素出现的问题 http://caiceclb.iteye.com/blog/428085 div内部块级元素,比如p,div,设置外间距(margin)的话会怎样.本来还纳闷 ...
- 使用meaven打包过程中遇到的一些问题
开始使用如下代码进行打包 <build> <!-- mvn assembly:assembly -Dmaven.test.skip=true --> <plugins&g ...
- enum枚举类型 的用法
1.作为数组下标使用 enun box{pencil, ruler}; void main() { string s[2]; s[pencil]="pencil"; s[rule ...
- Linux学习之find命令
find是我们很常用的一个Linux命令,但是我们一般查找出来的并不仅仅是看看而已,还会有进一步的操作,这个时候exec的作用就显现出来了. exec解释: -exec 参数后面跟的是command ...
- Storm的数据可靠性(理论)
Storm的数据可靠性(理论) .note-content {font-family: "Helvetica Neue",Arial,"Hiragino Sans GB& ...
- javascript外部ファイル
function myFunction() { document.getElementById("demo").innerHTML = "Paragraph cha ...
- Octopress创建GitHub Pages——基于代码托管的静态博客
Github Pages是静态网页来的,官方也半认可了它的博客用途,代码挂在github上,随时都可以更改,算是不错的一种尝试,因为它是静态的,所以在表现上会自由得多,但是,同样因为它是静态的,管理上 ...
- Linux----给一个普通用户root权限
问题说明:linux可以通过useradd创建用户.那有没有想过.我们创建的用户怎么样才可以使它得到全部的root权限呢? 解决办法: 1.这是一个可以打80分的办法.就是编辑/etc/sudoers ...