【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 sorted arrays. The overall run time complexity should be O(log (m+n)).
解题思路:
这本身是个很简单的题目,但是题目要求他的复杂度为O(log(m+n)),就很有难度了。不过首先我们还是可以明确我们要用分治法。关键是怎么分治呢?我们有如下的思路(核心思想是求第k小):
class Solution:def findk(self, A, m, B, n, k):if m > n:return self.findk(B, n, A, m, k)if m == 0:return B[k-1]if k == 1:return min(A[0],B[0])pa = min(k / 2, m)pb = k - paif A[pa - 1] < B[pb - 1]:return self.findk(A[pa:], m - pa, B, n, k - pa)elif A[pa - 1] > B[pb - 1]:return self.findk(A, m, B[pb:], n - pb, k - pb)else:return A[pa - 1]# @return a floatdef findMedianSortedArrays(self, A, B):la = len(A)lb = len(B)l = la + lbif l % 2 == 0:return (self.findk(A, la, B, lb, l/2+1) + self.findk(A, la, B, lb, l/2))/2.0else:return self.findk(A, la, B, lb, l/2+1)
很容易证明这个想法的复杂度正是O(log(m+n))
【leetcode】Median of Two Sorted Arrays的更多相关文章
- 【leetcode】Median of Two Sorted Arrays(hard)★!!
There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted ...
- 【leedcode】 Median of Two Sorted Arrays
https://leetcode.com/problems/median-of-two-sorted-arrays/ There are two sorted arrays nums1 and num ...
- 【算法之美】求解两个有序数组的中位数 — leetcode 4. Median of Two Sorted Arrays
一道非常经典的题目,Median of Two Sorted Arrays.(PS:leetcode 我已经做了 190 道,欢迎围观全部题解 https://github.com/hanzichi/ ...
- LeetCode(3) || Median of Two Sorted Arrays
LeetCode(3) || Median of Two Sorted Arrays 题记 之前做了3题,感觉难度一般,没想到突然来了这道比较难的,星期六花了一天的时间才做完,可见以前基础太差了. 题 ...
- LeetCode 4. Median of Two Sorted Arrays & 归并排序
Median of Two Sorted Arrays 搜索时间复杂度的时候,看到归并排序比较适合这个题目.中位数直接取即可,所以重点是排序. 再来看看治阶段,我们需要将两个已经有序的子序列合并成一个 ...
- leetCode之Median of Two Sorted Arrays
[题目描述] There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of ...
- LeetCode 4 Median of Two Sorted Arrays (两个数组的mid值)
题目来源:https://leetcode.com/problems/median-of-two-sorted-arrays/ There are two sorted arrays nums1 an ...
- Leetcode 4. Median of Two Sorted Arrays(二分)
4. Median of Two Sorted Arrays 题目链接:https://leetcode.com/problems/median-of-two-sorted-arrays/ Descr ...
- 第三周 Leetcode 4. Median of Two Sorted Arrays (HARD)
4. Median of Two Sorted Arrays 给定两个有序的整数序列.求中位数,要求复杂度为对数级别. 通常的思路,我们二分搜索中位数,对某个序列里的某个数 我们可以在对数时间内通过二 ...
随机推荐
- 基于fastweixin的微信开发环境搭建(一)
由于公司业务需要,开发微信版本,才开始接触微信公众平台.在github折腾了几天,试过好几个微信sdk,最终选择fastweixin.个人觉得这个框架还是值得使用的,使用也简单.那么问题来了,很多人想 ...
- 移动端自适应:flexible.js可伸缩布局使用
http://caibaojian.com/flexible-js.html 阿里团队开源的一个库.flexible.js,主要是实现在各种不同的移动端界面实现一稿搞定所有的设备兼容自适应问题. 实现 ...
- 深夜用git真是醉了
t吐槽一下那些 感觉命令行高效的傻逼们 我只想吃个牛肉串 你让我先学会宰牛? 命令不是对底层代码的封装? 程序的终极奥义就是把很多复杂的东西,封装到一个按钮就能完成 .叫做简为美. 浪费别人的时间等 ...
- React.js入门笔记(再续):评论框的实现
本案例来自React.js中文官网对应内容. 一. 运行环境 <link rel="stylesheet" type="text/css" href=&q ...
- ASP.NET 判断客户端是否为手机的函数
http://www.cnblogs.com/cxd4321/p/3795911.html
- JS阻止冒泡事件以及默认事件发生的简单方法
如果<p>是在<div>里面,那么呢,<P>有一个onclick事件,<div>也有onclick事件,为了触发<P>的点击事件时,不触发父 ...
- 简单的 MessageBox
有时候我们只是想实现一个消息框,给用户一些文字提醒,就像javascript的alert那样.没必要因此动用那些庞大的GUI库,下面是几种轻快的实现方法. 1. ctypes import ctype ...
- mysql中distinct的用法
本事例实验用表task,结构如下 MySQL> desc task; +-------------+------------+------+-----+-------------------+- ...
- 利用Levenshtein Distance (编辑距离)实现文档相似度计算
1.首先将word文档解压缩为zip /** * 修改后缀名 */ public static String reName(String path){ File file=new File(path) ...
- IIS部署遇到的一些问题
IIS部署时候会遇到一些具体的问题,记录一下.此处的部署环境为Windows Server 2012 64位版本 1.基本部署:角色和功能管理-->web服务器,勾选相应的服务与功能,然后安装即 ...