题目: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. 输出数组里面第N大的数

    好像有些大公司出过面试题:找出数组里面第N大的数,当然有点变化,但本质部分是这样的. 要求是不能排序,时间复杂度不能超过O(n^2) 思路很多,我暂时就只会快排衍生的那种.如果对快速排序不太熟悉了,建 ...

  2. -bash: ls: command not found

    在iMac下ls既然command not found,查找了下 原因:在设置环境变量时,编辑profile文件没有写正确,导致在命令行下 ls等命令不能够识别.解决方案: 直接在控制台下  expo ...

  3. QT:QBitArray

    QbitArray类提供位操作序列. include<QBitArray> 公有函数: QBitArray ()   QBitArray  ( int  size , bool  valu ...

  4. 如何实现windows和linux之间的文件传输

    2010-04-25 18:10 如何实现windows和linux之间的文件传输 如果想从windows中传送大量文件到Linux中,想必会难倒部分Linux初学者,尤其是文件很大时.我曾试过在li ...

  5. Java编程最差代码

    字符串连接误用 错误的写法:  String s = ""; for (Person p : persons) { s += ", " + p.getName( ...

  6. fedora 23 安装genymotion解决方案

    由于学习android开发,都说genymotion模拟器给力,我就尝试了下,安装过程参考 :但出现这种错误:缺少库 libjpeg.so.8 ,我就各种goole和百度找到库(链接地址),解压之后放 ...

  7. modelsim仿真时让状态机波形显示状态的名字

    在使用Verilog编写有限状态机等逻辑的时候,状态机的各个状态通常以参数表示(如IDLE等).当使用ModelSim仿真的时候,状态机变量在wave窗口中以二进制编码的形式显示,如下面所示,这种显示 ...

  8. Android开发之Intent跳转到系统应用中的拨号界面、联系人界面、短信界面

    现在开发中的功能需要直接跳转到拨号.联系人.短信界面等等,查找了很多资料,自己整理了一下. 1.跳转到拨号界面,代码如下: 1)直接拨打 Intent intentPhone = new Intent ...

  9. Unity3D 之UGUI 文本框和编辑框

    这里来讲解一下unity3D自带的UI功能,自带的UI也叫UGUI功能非常的强大,比起NGUI,更加的灵活,让用户能够更加容易的去使用. 首先创建一个文本Text 然后是文本相对应的属性 这里的属性比 ...

  10. 二维码QRCode

    package com.aig.ecompass.ecard; import java.awt.image.BufferedImage; import java.io.File; import jav ...