题目: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))

题解:

1、自己想得思路:构建一个list,然后比较各数的大小,将其插入到合适的位置

class Solution:
# @param {integer[]} nums1
# @param {integer[]} nums2
# @return {float}
def findMedianSortedArrays(self, nums1, nums2):
nums = nums1[:] # 构建一个list,并将nums1的值赋给它
x = len(nums1)
y = len(nums2)
for i in range(x + y):
if i < len(nums):
if len(nums2) == 0: break # 如果nums2没有数了就跳出
elif nums[i] < nums2[0]:
continue
else: # 否则将nums2[0]插入到i位置
num = nums2.pop(0)
nums.insert(i, num)
else: break
nums.extend(nums2)
n = len(nums)/2 # 输出结果
if len(nums)%2 == 0: return (nums[n] + nums[n-1])/2.0
else: return nums[n]

2、参考网上的解题思路(http://c4fun.cn/blog/2014/03/20/leetcode-solution-02/)

用求两个有序数组的第K大数的方法:

假设A数组中取第X个数, B数组中取第Y个数,并且满足X+Y=K, 若A[X] < B[Y],则比A[X]小的数必然少于K个,也就是说A[1]到A[X]都比第K个数要小,可以舍弃掉然后求第K-X小的数,反之亦然

class Solution:
def findMedianSortedArrays(self, A, B):
totlen = len(A) + len(B)
if (1 & totlen): # 通过位运算判断奇偶数,nice
return self.findK(A, B, (totlen+1)/2)
else:
return (self,findK(A, B, totlen/2) + self.findK(A, B, totlen/2+1))/2.0 def findK(self, A, B, K):
la, lb, pa, pb = len(A), len(B), min(K/2, len(A)), K - (min(K/2, len(A)))
if (la > lb): return self.findK(B, A, K)
if (la == 0): return B[K-1]
if (K == 1): return min(A[0], B[0])
if A[pa-1] < B[pb-1]: return self.findK(A[pa:], B, K-pa)
elif A[pa-1] > B[pb-1]: return self.findK(A, B[pb:], K-pb)
else: return A[pa-1]

Leetcode 解题 Median of Two sorted arrays的更多相关文章

  1. LeetCode(3) || Median of Two Sorted Arrays

    LeetCode(3) || Median of Two Sorted Arrays 题记 之前做了3题,感觉难度一般,没想到突然来了这道比较难的,星期六花了一天的时间才做完,可见以前基础太差了. 题 ...

  2. 【算法之美】求解两个有序数组的中位数 — leetcode 4. Median of Two Sorted Arrays

    一道非常经典的题目,Median of Two Sorted Arrays.(PS:leetcode 我已经做了 190 道,欢迎围观全部题解 https://github.com/hanzichi/ ...

  3. LeetCode 4 Median of Two Sorted Arrays (两个数组的mid值)

    题目来源:https://leetcode.com/problems/median-of-two-sorted-arrays/ There are two sorted arrays nums1 an ...

  4. Leetcode 4. Median of Two Sorted Arrays(二分)

    4. Median of Two Sorted Arrays 题目链接:https://leetcode.com/problems/median-of-two-sorted-arrays/ Descr ...

  5. LeetCode 4. Median of Two Sorted Arrays & 归并排序

    Median of Two Sorted Arrays 搜索时间复杂度的时候,看到归并排序比较适合这个题目.中位数直接取即可,所以重点是排序. 再来看看治阶段,我们需要将两个已经有序的子序列合并成一个 ...

  6. 第三周 Leetcode 4. Median of Two Sorted Arrays (HARD)

    4. Median of Two Sorted Arrays 给定两个有序的整数序列.求中位数,要求复杂度为对数级别. 通常的思路,我们二分搜索中位数,对某个序列里的某个数 我们可以在对数时间内通过二 ...

  7. Leetcode 4. Median of Two Sorted Arrays(中位数+二分答案+递归)

    4. Median of Two Sorted Arrays Hard There are two sorted arrays nums1 and nums2 of size m and n resp ...

  8. LeetCode 004 Median of Two Sorted Arrays

    题目描述:Median of Two Sorted Arrays There are two sorted arrays A and B of size m and n respectively. F ...

  9. 【leetcode】Median of Two Sorted Arrays

    题目简述: There are two sorted arrays A and B of size m and n respectively. Find the median of the two s ...

随机推荐

  1. PetShop 4.0学习笔记:消息队列MSMQ

    直到今天才知道,在我们每天都在用的Window系统里还有这么好用的一个编程组件:消息队列.它能够解决在大数据量交换的情况下的性能问题,特别是BS系统的数据库性能.而且它的异步处理方式能给程序员最大的便 ...

  2. [Whole Web] [AngularJS] Localize your AngularJS Application with angular-localization

    It is best to start your application's localization efforts early in development, even if you only s ...

  3. DTrace patch for Python 2.7.x and 3.x

    DTrace patch for Python 2.7.x and 3.x Última Actualización: 21 de septiembre de 2015 https://www.jce ...

  4. Sample Ant Build File - WAR--reference

    I am using the Spring SimpleFormController example to illustrate the build process. The figure below ...

  5. JavaScript入门(4)

    一.JS能做什么? 1.增强页面动态效果(如:下拉菜单.图片轮播.信息滚动等) 2.实现页面与用户之间的实时.动态交互(如:用户注册.登录验证等) 什么是变量?http://www.cnblogs.c ...

  6. 幻灯片の纯CSS,NO JavaScript

    之前就遇到有人问,不用js,纯css实现幻灯片. 那么对于使用纯的css + html 怎样来实现幻灯片呢?下面有几种方法可供参考,有些还不成熟. 方案一:利用css3的animation 例子传送门 ...

  7. String filePath = request.getSession().getServletContext().getRealPath("/");这句话返回的路径是什么,解释下getRealPath("/")函数中的"/"表示什么意思

    request.getSession().getServletContext() 获取的是Servlet容器对象,相当于tomcat容器了.getRealPath("/") 获取实 ...

  8. SQL Server系统表sysobjects介绍与使用(转)

    SQL Server系统表sysobjects介绍与使用 关于SQL Server数据库的一切信息都保存在它的系统表格里.我怀疑你是否花过比较多的时间来检查系统表格,因为你总是忙于用户表格.但是,你可 ...

  9. c#md5与SHA1验证函数

    /// <summary> /// MD5验证函数 /// </summary> /// <param name="fileName">文件的路 ...

  10. java 环境变量配置

    1.我的电脑 2.属性 3.高级 4.环境变量 5.新建 6.JAVA_HOME: C:\Program Files (x86)\Java 7.CLASSPATH: .;%JAVA_HOME%\;%J ...