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. 【转】Android 图层引导帮助界面制作

    2012-11-02 10:31 1979人阅读 评论(0) 收藏 举报 原文:http://www.cnblogs.com/beenupper/archive/2012/07/18/2597504. ...

  2. sql汉字转拼音

    /*创建取拼音首字母函数*/ create function [dbo].[fn_ChineseToSpell](@strChinese varchar(500)='') returns varcha ...

  3. S全选功能代码

    JS全选功能代码优化 2014-06-26 00:00 by 龙恩0707, 470 阅读, 3 评论, 收藏, 编辑 JS全选功能代码优化 最近在看javascript MVC那本书,也感觉到自己写 ...

  4. Linq to sql 结

    ----左链接 var LeftJoin = from emp in ListOfEmployees join dept in ListOfDepartment on emp.DeptID equal ...

  5. 最小生成树算法prim and kruskal

    一.最小生成树定义:  从不同顶点出发或搜索次序不同,可得到不同的生成树  生成树的权:对连通网络来说,边附上权,生成树也带权,我们把生成树各边的权值总和称为生成树的权  最小代价生成树:在一个连通网 ...

  6. IIS 7.5配置PHP站点

    准备工作 首先下载并解压PHP程序文件,比如解压到C:/PHP/,不需要安装.IIS安装略. 第一步:添加ISAPI筛选器和CGI功能 控制面板—>程序和功能—>打开或关闭Windows功 ...

  7. 用py2exe打包pyqt4出现的问题(转)

    使用pyqt完成窗体界面很方便,但是打包成exe之后会有问题,在网上找到解决办法如下: Another Solution to the same problem: from distutils.cor ...

  8. poj2488骑士之旅

    题目大意:国际象棋里面的马,有那么8种跳法,然后题目给出一个棋盘的大小p*q, 求有没有路线可以使得这个马能把整个棋盘的格全部走一遍,有的话按照字典序将第一条路线打印出来. 注意:国际象棋是行是数字, ...

  9. Cracking the Coding Interview(Stacks and Queues)

    Cracking the Coding Interview(Stacks and Queues) 1.Describe how you could use a single array to impl ...

  10. 使用EntityFramework持久化聚合

    目录 背景使用EntityFramework持久化聚合备注 背景返回目录 DDD中只有聚合根可以有仓储,仓储负责整个聚合持久化的相关生命周期,在不使用工作单元或POCO的情况下,我们可以让Order内 ...