【LeeetCode】4. 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 sorted arrays. The overall run time complexity should be O(log (m+n)).
Example 1:
nums1 = [1, 3]
nums2 = [2] The median is 2.0
Example 2:
nums1 = [1, 2]
nums2 = [3, 4] The median is (2 + 3)/2 = 2.5
题意:找出两个数组的中间值
思路:首先是想利用像归并排序的方法,排序完成后,选出中间值,但是时间复杂度就不是O(log(m+n))了
从网上找到另一种方法,我写下我自己理解的思路:
找出中间值,也就是找出第K小的数
借用二分查找法的思路,第k/2小的值在或者不在短的数组中,就比如短的数组事nums1。
如果k/2在短的数组中,就比较nums1[k/2]和nums2[k/2],比如如果nums1[k/2]更小,那么就可以排除掉nums1中,索引k/2之前的所有数。
如果k/2不在短的数组中,就比较nums1[len(nums1)]和nums2[k-len(nums1)]的值,谁更小,就排除掉哪个数组
如果两个值相等,就找到了!
class Solution(object):
def findMedianSortedArrays(self, nums1, nums2):
"""
:type nums1: List[int]
:type nums2: List[int]
:rtype: float
"""
len1 = len(nums1)
len2 = len(nums2)
l = len1 + len2
if l&0x1 == 1:
return self.findK(nums1,len1,nums2,len2,l/2+1)
else:
return (self.findK(nums1,len1,nums2,len2,l/2)+self.findK(nums1,len1,nums2,len2,l/2+1))*1.0/2 def findK(self,nums1,len1,nums2,len2,k):
if len1>len2:
return self.findK(nums2,len2,nums1,len1,k)
if len1 == 0:
return nums2[k-1]
if k == 1:
return min(nums1[0],nums2[0])
mid = k/2
pa = min(mid,len1)
pb = k-pa
if nums1[pa-1] < nums2[pb-1]:
return self.findK(nums1[pa:],len1-pa,nums2,len2,k-pa)
elif nums1[pa-1] > nums2[pb-1]:
return self.findK(nums1,len1,nums2[pb:],len2-pb,k-pb)
else:
return nums1[pa-1]
【LeeetCode】4. Median of Two Sorted Arrays的更多相关文章
- 【LeetCode】4. Median of Two Sorted Arrays (2 solutions)
Median of Two Sorted Arrays There are two sorted arrays A and B of size m and n respectively. Find t ...
- 【leetcode】4. Median of Two Sorted Arrays
题目描述: There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of t ...
- 【medium】4. Median of Two Sorted Arrays 两个有序数组中第k小的数
There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...
- 【LeetCode】004. Median of Two Sorted Arrays
题目: There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the ...
- 【LeetCode】4.Median of Two Sorted Arrays 两个有序数组中位数
题目: There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the ...
- 【LeetCode】4. Median of Two Sorted Arrays(思维)
[题意] 给两个有序数组,寻找两个数组组成后的中位数,要求时间复杂度为O(log(n+m)). [题解] 感觉这道题想法非常妙!! 假定原数组为a,b,数组长度为lena,lenb. 那么中位数一定是 ...
- 【LeetCode】4. Median of Two Sorted Arrays 寻找两个正序数组的中位数
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:数组,中位数,题解,leetcode, 力扣,python ...
- 【一天一道LeetCode】#4 Median of Two Sorted Arrays
一天一道LeetCode (一)题目 There are two sorted arrays nums1 and nums2 of size m and n respectively. Find th ...
- leetcode-【hard】4. Median of Two Sorted Arrays
题目 There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the ...
随机推荐
- 读书笔记—CLR via C#线程27章节
前言 这本书这几年零零散散读过两三遍了,作为经典书籍,应该重复读反复读,既然我现在开始写博了,我也准备把以前觉得经典的好书重读细读一遍,并且将笔记整理到博客中,好记性不如烂笔头,同时也在写的过程中也可 ...
- Smarty注释代码
所有的smarty模板标签都被加上了定界符. 默认情况下是 { 和},但它们是可被改变的. 例如,我们假定你在使用默认定界符. 在smarty里,所有定界符以外的内容都是静态输出的,或者称之为不可改变 ...
- Android L新控件RecyclerView简介
Android L是android进化史上的里程碑,尽管还没有正式发布4.5或者5.0,但预览版也同样精彩. 这篇文章只是另外一篇博客的总结性翻译,能够读懂原文的,可以点开这个链接去阅读精彩的原文:h ...
- bootstrap错误警告信息提示
bootstrap提供了成功执行.警告和错误信息的样式. 在使用该功能的时候需要引入以下几个文件: bootstrap.css jquery.js(需放在bootstrap.js之前) bootstr ...
- ASP.NET MVC4中使用NHibernate
ASP.NET MVC4中使用NHibernate 1:下载安装NHibernate 打开 VS 2012新建一个 MVC4项目. 在项目名称上右击选择Manage NuGet Packages.你会 ...
- AngularJS的工作原理
AngularJS的工作原理 个人觉得,要很好的理解AngularJS的运行机制,才能尽可能避免掉到坑里面去.在这篇文章中,我将根据网上的资料和自己的理解对AngularJS的在启动后,每一步都做了些 ...
- 安装mysql-python报错:UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 65: ordinal not in range(128)
安装mysql-python报错: UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 65: ordinal n ...
- Load ContextCLR 探测
目录 背景Load ContextCLR 探测过程弱签名程序集的探测过程强签名程序集的探测过程Default ContextLoad-From ContextNo ContextRelfection- ...
- Vnix的Logo设计
又捣鼓了一下Logo,感觉Ascii Design碉堡了.下面贴出几款Logo以供观赏,欢迎投票. ## ## ## ## #### ## ## ## ## ### ## ## ## ## ## ## ...
- OAuth的一个.NET开源实现
从编译DotNetOpenAuth中学到的程序集强签名知识 OAuth的一个.NET开源实现,官方网站:http://dotnetopenauth.net/ . 从GitHub签出DotNetOpen ...