【LeeetCode】4. Median of Two Sorted Arrays
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)).
Example 1:
nums1 = [1, 3]
nums2 = [2] The median is 2.0
Example 2:
nums1 = [1, 2]
nums2 = [3, 4] The median is (2 + 3)/2 = 2.5
题意:找出两个数组的中间值
思路:首先是想利用像归并排序的方法,排序完成后,选出中间值,但是时间复杂度就不是O(log(m+n))了
从网上找到另一种方法,我写下我自己理解的思路:
找出中间值,也就是找出第K小的数
借用二分查找法的思路,第k/2小的值在或者不在短的数组中,就比如短的数组事nums1。
如果k/2在短的数组中,就比较nums1[k/2]和nums2[k/2],比如如果nums1[k/2]更小,那么就可以排除掉nums1中,索引k/2之前的所有数。
如果k/2不在短的数组中,就比较nums1[len(nums1)]和nums2[k-len(nums1)]的值,谁更小,就排除掉哪个数组
如果两个值相等,就找到了!
class Solution(object):
def findMedianSortedArrays(self, nums1, nums2):
"""
:type nums1: List[int]
:type nums2: List[int]
:rtype: float
"""
len1 = len(nums1)
len2 = len(nums2)
l = len1 + len2
if l&0x1 == 1:
return self.findK(nums1,len1,nums2,len2,l/2+1)
else:
return (self.findK(nums1,len1,nums2,len2,l/2)+self.findK(nums1,len1,nums2,len2,l/2+1))*1.0/2 def findK(self,nums1,len1,nums2,len2,k):
if len1>len2:
return self.findK(nums2,len2,nums1,len1,k)
if len1 == 0:
return nums2[k-1]
if k == 1:
return min(nums1[0],nums2[0])
mid = k/2
pa = min(mid,len1)
pb = k-pa
if nums1[pa-1] < nums2[pb-1]:
return self.findK(nums1[pa:],len1-pa,nums2,len2,k-pa)
elif nums1[pa-1] > nums2[pb-1]:
return self.findK(nums1,len1,nums2[pb:],len2-pb,k-pb)
else:
return nums1[pa-1]
【LeeetCode】4. Median of Two Sorted Arrays的更多相关文章
- 【LeetCode】4. Median of Two Sorted Arrays (2 solutions)
Median of Two Sorted Arrays There are two sorted arrays A and B of size m and n respectively. Find t ...
- 【leetcode】4. Median of Two Sorted Arrays
题目描述: There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of t ...
- 【medium】4. Median of Two Sorted Arrays 两个有序数组中第k小的数
There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...
- 【LeetCode】004. Median of Two Sorted Arrays
题目: There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the ...
- 【LeetCode】4.Median of Two Sorted Arrays 两个有序数组中位数
题目: There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the ...
- 【LeetCode】4. Median of Two Sorted Arrays(思维)
[题意] 给两个有序数组,寻找两个数组组成后的中位数,要求时间复杂度为O(log(n+m)). [题解] 感觉这道题想法非常妙!! 假定原数组为a,b,数组长度为lena,lenb. 那么中位数一定是 ...
- 【LeetCode】4. Median of Two Sorted Arrays 寻找两个正序数组的中位数
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:数组,中位数,题解,leetcode, 力扣,python ...
- 【一天一道LeetCode】#4 Median of Two Sorted Arrays
一天一道LeetCode (一)题目 There are two sorted arrays nums1 and nums2 of size m and n respectively. Find th ...
- leetcode-【hard】4. Median of Two Sorted Arrays
题目 There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the ...
随机推荐
- jQuery 1.9 移除了 $.browser 的替代方法
jQuery 从 1.9 版开始,移除了 $.browser 和 $.browser.version , 取而代之的是 $.support . 在更新的 2.0 版本中,将不再支持 IE 6/7/8. ...
- ref引用类型,数组型参数,out输出参数
ref和out的相同点和不同点 共同点:都是引用传递不同点:ref的参数在调用之前一定要赋值,在方法调用的过程中可以不要赋值. out的参数在调用之前可以不赋值,在方法调用的过程中一定要赋值. ...
- bootstrap错误警告信息提示
bootstrap提供了成功执行.警告和错误信息的样式. 在使用该功能的时候需要引入以下几个文件: bootstrap.css jquery.js(需放在bootstrap.js之前) bootstr ...
- cefsharp实现javascript回调C#方法
在构建完WebView webView = new WebView(url)后,即可调用RegisterJsObject方法来注册一个js对象,从而前端的javascript就可以访问这个对象,调用定 ...
- C#集合基础与运用
C#集合基础与运用 C#集合基础与运用 1. 集合接口与集合类型............................................... 1 (1) 集合的命名空间..... ...
- ecshop下启用QQ在线服务,并能实时更新QQ在线状态
按照 http://blog.csdn.net/zurich1979/article/details/9082201 可轻松实现在线客服, 但是使用后发现一个问题,那就是这种情况下在线客服不能根据QQ ...
- WebSocket在ASP.NET MVC4中的简单实现
WebSocket在ASP.NET MVC4中的简单实现 2013-12-21 20:48 by 小白哥哥, 810 阅读, 3 评论, 收藏, 编辑 WebSocket 规范的目标是在浏览器中实现和 ...
- C++中文件的操作
#include <iostream> #include <fstream> using namespace std; int main() { char s[27],m[27 ...
- 10.26最后的模拟DAY2 改造二叉树[中序遍历+严格递增的最长不下降子序列]
改造二叉树 [题目描述] 小Y在学树论时看到了有关二叉树的介绍:在计算机科学中,二叉树是每个结点最多有两个子结点的有序树.通常子结点被称作“左孩子”和“右孩子”.二叉树被用作二叉搜索树和二叉堆.随后他 ...
- 随机函数Surprising
之前写了个用来抽取1-54号的随机函数,发现30-40出现的情况很大,就在果壳上提问了一下//听取了某个大神的建议循环了10000次之后惊喜的发现这样写出现了一大堆相同的数字! 之后有个很神大牛解答了 ...