题目: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. JAVA中List转换String,String转换List,Map转换String,String转换Map之间的转换类

    <pre name="code" class="java"></pre><pre name="code" cl ...

  2. 分布式数据库中间件–(3) Cobar对简单select命令的处理过程

    友情提示:非原文链接可能会影响您的阅读体验,欢迎查看原文.(http://blog.geekcome.com) 原文地址:http://blog.geekcome.com/archives/284 在 ...

  3. java的定时器用法

    java定时器的使用 定时器类Timer在java.util包中.使用时,先实例化,然后使用实例的schedule(TimerTask task, long delay)方法,设定指定的任务task在 ...

  4. J2EE之ServletContext读取资源文件

    ServletContext读取资源文件内容的方式有两种: 方法1. public void doGet(HttpServletRequest request, HttpServletResponse ...

  5. MyPhone

    based on H323plus project. https://github.com/muggot/myphone3/tree/master/myphone3

  6. SECURITY_ATTRIBUTES 设置低权限

    Windows 从 Vista 開始又一次改动了其系统的权限管理机制,于是如今就会碰到一些 xp 能过而 win7 不能过的代码.比方 Service 程序和一般应用程序用共享内存的方式来通讯,Cre ...

  7. 标准I/O的替代软件

    标准I/O并不完善. 标准I/O库的一个不足之处是效率不高,这与它需要复制的数据量有关.当使用每次一行函数fgets和fputs时,通常需要复制两次数据:一次是在内核和标准I/O缓冲之间(当调用rea ...

  8. MVC - 基础

    什么是MVC模式 传统的WebForm发展到如今出现不少的缺陷 比如为了解决Http的无状态 WebForm模式使用了ViewState来保存客户端和服务端数据 过量的使用则会造成页面臃肿不堪 大量服 ...

  9. html笔记04:在html之中导入css两种常见方法

    1.导入式: <html> <head> <title></title> <style type="text/css"> ...

  10. C#操控条形码扫描枪

    // 条码扫描器 // 窗体部分相关代码: using System; using System.Collections.Generic; using System.ComponentModel; u ...