[LeetCode][Python]Median of Two Sorted Arrays
# -*- coding: utf8 -*-
'''
https://oj.leetcode.com/problems/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)). ===Comments by Dabay===
先计算中位数在合并后数组中的什么坐标为medium_pos。
同时考虑到,合并后数组的元素总数是奇数偶数的不同情况,用一个变量last来记录寻找到中位数的游标停下来的位置。
如果是奇数,这个last没有用;如果是偶数,用这个last和下一个可能的数求一个平均数。
''' class Solution:
# @return a float
def findMedianSortedArrays(self, A, B):
len_a, len_b = len(A), len(B)
i = j = 0
medium_pos = (len_a + len_b) / 2
counter = 0
last = None
while counter < medium_pos:
if i < len_a and j < len_b:
if A[i] < B[j]:
last = A[i]
i = i + 1
else:
last = B[j]
j = j + 1
elif i < len_a:
last = A[i]
i = i + 1
elif j < len_b:
last = B[j]
j = j + 1
counter = counter + 1
if (len_a + len_b) % 2 == 0:
if i < len_a and j < len_b:
return (last + min(A[i], B[j])) / 2.0
elif i == len_a:
return (last + B[j]) / 2.0
elif j == len_b:
return (last + A[i]) / 2.0
else:
if i < len_a and j < len_b:
return min(A[i], B[j])
elif i == len_a:
return B[j]
elif j == len_b:
return A[i] def main():
s = Solution()
A = [1, 2, 3, 4, 5]
B = []
print s.findMedianSortedArrays(A, B) if __name__ == "__main__":
import time
start = time.clock()
main()
print "%s sec" % (time.clock() - start)
[LeetCode][Python]Median of Two Sorted Arrays的更多相关文章
- 【算法之美】求解两个有序数组的中位数 — 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 (两个数组的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 4. Median of Two Sorted Arrays
https://leetcode.com/problems/median-of-two-sorted-arrays/ There are two sorted arrays nums1 and num ...
随机推荐
- shopncv4 短信接口 提供商 中国短信网
前提是在后台开启手机注册功能:具体是在设置->账号同步->手机短信 里开启. 修改 siteroot\core\framework\libraries\sms.php 修改 sit ...
- 给新建的Cocos2d-x 3.X的Win32工程添加CocoStudio库
1.我们在VS中找到"解决方案资源管理器", 在解决方案"HelloCocos"上点击右键, 选择添加现有项目. 在弹出的对话框中选择************* ...
- 点击后改变css属性
在html中: <div class="formbuilder"> <div class="active">Heading< ...
- EMV标准
EMV标准是由国际三大银行卡组织--Europay(欧陆卡,已被万事达收购).MasterCard(万事达卡)和Visa(维萨)共同发起制定的银行卡从磁条卡向智能IC卡转移的技术标准,是基于IC卡的金 ...
- 给ecshop后台增加管理功能页面
给ecshop后台增加管理功能页面 比如我们增加一个统计报表叫做 物流费用统计报表 放在后台“报表统计”栏目中 具体操作步骤: 第一步,我们要添加一个菜单到后台,然后设置语言项,最后设置权限,这样,后 ...
- Centos6.5 telnet wireshark
yum -y install telnet-server telnet vim /etc/xinted.d/telnet disable = no vim /etc/pam.d/remote #aut ...
- [Linux] 解压缩 tar 命令详解
在Linux环境软件安装过程中通常需要用到解压命令,故在此总结下,以方便以后使用,若有不对之处,欢迎指正. 1. 文件压缩 通过压缩算法将文件的体积缩小,同时会将多个文件合并成至一起方便 ...
- 网易云课堂_程序设计入门-C语言_期末考试编程题
1 字数统计(10分) 题目内容: 你的程序要读入一篇英文文章,然后统计其中的单词数来输出.需要统计的数据为: 总的单词数量: 含有1个字母到10个字母的单词的数量. 单词和单词的间隔是由以下标点符号 ...
- GCDAsyncUdpSocket的使用
tips: 要注意服务器端口与客户端端口的区别,如果客户端绑定的是服务器的端口,那么服务器发送的消息就会一直发送给服务器.
- iOS第三方库-CocoaLumberjack-DDLog (转)
原文:http://blog.sina.com.cn/s/blog_7b9d64af0101kkiy.html 发现一个,很厉害的小工具,让xCode控制台输出文本有颜色! 闲话不说,上代码. 大概需 ...