Leetcode 解题 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))
题解:
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的更多相关文章
- 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.(PS:leetcode 我已经做了 190 道,欢迎围观全部题解 https://github.com/hanzichi/ ...
- 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 & 归并排序
Median of Two Sorted Arrays 搜索时间复杂度的时候,看到归并排序比较适合这个题目.中位数直接取即可,所以重点是排序. 再来看看治阶段,我们需要将两个已经有序的子序列合并成一个 ...
- 第三周 Leetcode 4. Median of Two Sorted Arrays (HARD)
4. Median of Two Sorted Arrays 给定两个有序的整数序列.求中位数,要求复杂度为对数级别. 通常的思路,我们二分搜索中位数,对某个序列里的某个数 我们可以在对数时间内通过二 ...
- 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 ...
- 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 ...
- 【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 ...
随机推荐
- linux下.run文件的安装与卸载
linux下.run文件的安装与卸载 .run文件的安装很简单,只需要为该文件增加可执行属性,即可执行安装 以 virtualbox 的安装文件 virtualbox-3.1.6-59338-Li ...
- 设置 Linux 文件和文件夹权限的方法
1.字符表示法 类型 操作 权限 u + r g - w o = x a 类型代表改动权限的对象,u表示文件的用户.g表示文件全部者的群组,o表示非用户组用户.a表示ugo 操作 + 表示添加权限,- ...
- ubuntu14.04 64bit 安装 && 破解quartus13.0 记录
安装文件:Quartus-13.0.0.156-linux.iso Quartus-13.0.0.156-devices-1.iso 1.挂载:sudo mount -o lo ...
- linux安全加固
1 . BIOS 你应该总是在系统启动的时候设置一个BIOS 密码和禁用从CD-ROM 和软盘引导,这将可以防止一些人未经允许访问你的系统和更改BIOS 设置 2 .sshd 服务 SSH 是一个协议 ...
- redis持久化(摘录)
redis是一个支持持久化的内存数据库,也就是说redis需要经常将内存中的数据同步到磁盘来保证持久化.redis支持两种持久化方式,一种是 Snapshotting(快照)也是默认方式,另一种是Ap ...
- Objective-c中autorelease的释放时机
如果你使用过MRR,autorelease这个关键字应该是太熟悉了,每次在我们生成一个新的对象返回时,都需要向这个对象发送autorelease消息,目的是为了延时释放创建的对象.那到底是在什么时候, ...
- MVC小系列(十六)【在控制器级别或具体Action级别上动态设定模板页(Layout)】
方法就是使用:ActionFilterAttribute它的几个方法:OnActionExecuted,OnActionExecuting,OnResultExecuted和OnResultExecu ...
- 【JAVA错误笔记】 - c3p0问题java.lang.NoClassDefFoundError:com.mchange.v2.ser.Indirector
错误描述:java.lang.NoClassDefFoundError:com.mchange.v2.ser.Indirector 原因分析: 这是c3p0的一个错误信息,我们在下载 c3p0时候,z ...
- Spring.net架构示例(含Aop和Ioc)源码
最近写了一个Spring.net的架构. 一.架构主图 架构图的数据流程走向是: UI层=>UILogic>=>Service>Business=>DataAccess ...
- spring定时任务的配置
定时任务配置分为三个步骤: 1.定义任务 2.任务执行策略配置 3.启动任务 1.定义任务 <!--要定时执行的方法--> <bean id="testTaskJob&qu ...