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的更多相关文章

  1. 【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 ...

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

  3. 【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 ...

  4. 【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 ...

  5. 【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 ...

  6. 【LeetCode】4. Median of Two Sorted Arrays(思维)

    [题意] 给两个有序数组,寻找两个数组组成后的中位数,要求时间复杂度为O(log(n+m)). [题解] 感觉这道题想法非常妙!! 假定原数组为a,b,数组长度为lena,lenb. 那么中位数一定是 ...

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

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

  8. 【一天一道LeetCode】#4 Median of Two Sorted Arrays

    一天一道LeetCode (一)题目 There are two sorted arrays nums1 and nums2 of size m and n respectively. Find th ...

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

随机推荐

  1. 按照鬼哥学so变化,四,第一章的例子

    跟随鬼哥伦比亚科学so变化,四.第一章的例子 图纸/文化  听鬼哥说故事 ---------------------------------------------切割线--------------- ...

  2. iOS基础 - UIDynamic

    一.UIKit动力学 UIKit动力学最大的特点是将现实世界动力驱动的动画引入了UIKit,比如重力,铰链连接,碰撞,悬挂等效果,即将2D物理引擎引入了UIKit 注意:UIKit动力学的引入,并不是 ...

  3. T4 Template Overview

    T4 Template Overview   T4 Template的组成 指令区:为模板转换引擎提供指令,控制模板如何被处理 Ÿ   template:模板相关的属性,debug是否可以调试:hos ...

  4. PYTHON ASP FRAMEWORK

    Python 融于ASP框架   一.ASP的平反 想到ASP 很多人会说 “asp语言很蛋疼,不能面向对象,功能单一,很多东西实现不了” 等等诸如此类. 以上说法都是错误的,其一ASp不是一种语言是 ...

  5. Varint code

    Varint编码   LevelDB内部通过采用变长编码,对数据进行压缩来减少存储空间,采用CRC进行数据正确性校验.下面就对varint编码进行学习. 传统的integer是以32位来表示的,存储需 ...

  6. General Structure of Quartz.NET and How To Implement It

    General Structure of Quartz.NET and How To Implement It   General Structure of Quartz.NET and How To ...

  7. ios学习笔记第四天之官方文档总结

    start developing ios app today. 官方文档的体系结构为: 各层的主要框架图: objectice-c是动态语言 Objective-C 为 ANSI C 添加了下述语法和 ...

  8. jQuery全屏插件Textarea Fullscreen

    插件描述 Textarea Fullscreen是一个jquery插件,可以将textarea设置为全屏模式 使用方法 引用jquery.js,jquery.textareafullscreen.js ...

  9. jQuery实现鼠标移上弹出提示框,移出消失

    <TD>里有一行数据 "那片笑声让我想起......"  假设超出规定长度将用......代替, 而现在要通过鼠标移动到......上 显示全部内容,移出则消失.如下图 ...

  10. EasyUI tree扩展获取实心节点

    <script type="text/javascript"> //扩展 获得tree 的实心节点 $(function(){ $.extend($.fn.tree.m ...