题目: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. invoke-command

    远程执行命令: invoke-command -ComputerName $server -Credential $cred -ScriptBlock{param($server,$UserName, ...

  2. 【14】在资源管理类中小心copying行为

    1.为什么要使用资源管理类? 资源管理类的思路就是,栈上的对象,封装堆上分配的资源,确保一定会释放资源.auto_ptr和shared_ptr就是资源管理类,行为上像指针. 2.auto_ptr和sh ...

  3. ThinkPHP3.1新特性:Action参数绑定

    Action参数绑定功能提供了URL变量和操作方法的参数绑定支持,这一功能可以使得你的操作方法定义和参数获取更加清晰,也便于跨模块调用操作方法了.这一新特性对以往的操作方法使用没有任何影响,你也可以用 ...

  4. WPF WebBrowser

      XAML <Window x:Class="WpfApplication5.Window1"     xmlns="http://schemas.microso ...

  5. 基于smack的xmpp packet 重写

    基于Smack 实现Notification数据包.smack的类中有一个org.jivesoftware.smack.packet.IQ只需对他重写即可,在做的时候其实可以简单一点的,如果你使用ti ...

  6. [ES6] 21. ESNext, ES6-Shim & Node

    ES-Next: Esnextis similar to traceur, you can use command line to compile files. Install: npm instal ...

  7. iOS开发中一些常用的方法

    1.压缩图片 #pragma mark 处理图片 - (void)useImage:(UIImage *)image { NSLog(@"with-----%f heught-----%f& ...

  8. MVC - HtmlHelper类

    传统的Html元素不能和服务端数据进行绑定 HtmlHelper类提供了一系列的方法来生成Html元素 并可以实现与数据绑定在一起 然后生成Html Html.BeginForm(actionName ...

  9. iOS/object-c: 枚举类型 enum,NS_ENUM,NS_OPTIONS

    一般情况下,我们采用C风格的enum关键字可以定义枚举类型. enum{ UIViewAnimationTransitionNone, UIViewAnimationTransitionFlipFro ...

  10. Docker中开启sshd服务

    ssh服务安装 安装ssh服务 #yum install openssh-server -y 安装passwd(修改密码需要) #yum install passwd -y 修改sshd_config ...