# -*- 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的更多相关文章

  1. 【算法之美】求解两个有序数组的中位数 — leetcode 4. Median of Two Sorted Arrays

    一道非常经典的题目,Median of Two Sorted Arrays.(PS:leetcode 我已经做了 190 道,欢迎围观全部题解 https://github.com/hanzichi/ ...

  2. LeetCode(3) || Median of Two Sorted Arrays

    LeetCode(3) || Median of Two Sorted Arrays 题记 之前做了3题,感觉难度一般,没想到突然来了这道比较难的,星期六花了一天的时间才做完,可见以前基础太差了. 题 ...

  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 4. Median of Two Sorted Arrays

    https://leetcode.com/problems/median-of-two-sorted-arrays/ There are two sorted arrays nums1 and num ...

随机推荐

  1. CSS自学笔记(16):CSS3 用户界面

    CSS3中,也新增了一些关于用户界面的属性,这些属性可以重设元素或者盒子的尺寸.轮廓等等. 新增的部分属性的浏览器支持情况 属性 浏览器支持 resize IE Firefox Chrome Safa ...

  2. LGA(land grid array)

    产品应用 1.射频功放 2.加速度传感器 3.地磁传感器 可靠性 Reliability 高压蒸煮 PCT 121℃,100%RH,2atm,96hrs 高低温循环 TCT -55℃(15min)~1 ...

  3. Delphi SysErrorMessage 函数和系统错误信息表

    在看 API 文档时, 我们经常见到 GetLastError; 它可以返回操作后系统给的提示. 但 GetLastError 返回的只是一个信息代码, 如何返回对应的具体信息呢? FormatMes ...

  4. Flex中如何通过showAllDataTips属性使鼠标移动到图表时显示所有的数据Tips的例子

    原文 http://blog.minidx.com/2008/11/10/1616.html 接下来的例子演示了Flex中如何通过showAllDataTips属性,使鼠标移动到图表时显示所有的数据T ...

  5. hdu 2604 Queuing(矩阵快速幂乘法)

    Problem Description Queues and Priority Queues are data structures which are known to most computer ...

  6. Conscription

    Conscription Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) Total ...

  7. oracle 物化视图导入导出报错

    1.exp导出报EXP-00008: 遇到 ORACLE 错误 1455,ORA-01455: 转换列溢出整数数据类型 2.imp导入报.注: 表包括 ROWID 列, 其值可能已废弃,不是警告也不是 ...

  8. hdu 2054 A == B ? (java)

    问题: 考虑问题不周到.没有考虑到可能是小数并且存在 1.0=1.01=1的情况. 本题使用了BigDecimal类,此类适用于高精度的数此时攻克了小数和01=1的问题, 该类比較方式中n.equal ...

  9. INPUT[type=file]的change事件不触发问题

    在网页上要操作文件通常会使用INPUT[type=file]控件,但这个控件的设计很蛋疼.它不像其它编程语言中文件选择后会触发一个事件,只是让上面的文字改变,而这个改变可能会触发change事件而已. ...

  10. DevExpress中ChartControl柱状图(Bar)用法

    我的数据 代码如下: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 chartControl1.Series.Clear(); ...