# -*- 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. CPrimer Plus第12章 存储类、链接和内存管理随笔

    被static修饰的属于内部链接,不可被外部程序文件所使用一般而言,全局变量(文件作用域变量)具有静态存储期,局部变量(代码块作用域变量)具有自动存储期寄存器变量不能使用地址运算符因为被static修 ...

  2. HTML5新属性-----拖放

    最早引入JS拖放功能的是IE4,当时,网页中只有两种对象可以拖放:图像和某些文本.拖动图像时,把鼠标放在图像上,按住鼠标不放就可以拖动它.拖动文本时,需要选中文本,然后可以像拖动图像一样拖动被选中的文 ...

  3. MYSQL 二进制还原

    解决方法: mysqlbinlog bin_log_file_path_and_name | mysql -uroot -p 如: mysqlbinlog E:\DB\mysql_log\mysql_ ...

  4. Hibernate、批量操作数据

    Hibernate 批量操作数据可以使用两种方法实现 1.分批更新,每一小批同步一次数据: public void saveEmployee2(){ Session s=HibernateSessio ...

  5. Android Notivation的使用

    官方帮助文档:http://wear.techbrood.com/guide/topics/ui/notifiers/notifications.html 博文推荐:http://blog.csdn. ...

  6. Minimum Transport Cost(floyd+二维数组记录路径)

    Minimum Transport Cost Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/O ...

  7. Android——采用SQLiteDatabase操作SQLite数据库

    除了能够使用文件或SharedPreferences存储数据.还能够选择使用SQLite数据库存储数据. 在Android平台上,集成了一个嵌入式关系型数据库-SQLite,SQLite3支持 NUL ...

  8. 为何与0xff进行与运算

    为何与0xff进行与运算 在剖析该问题前请看如下代码 public static String bytes2HexString(byte[] b) { String ret = "" ...

  9. javascript高级知识点——临时作用域

    代码信息来自于http://ejohn.org/apps/learn/. 自执行,临时,函数 (function(){ var count = 0; })(); 这是一个简单的自执行匿名函数. 做一个 ...

  10. webview的配置

    WebSettings的常用方法介绍 WebSettings 通过webview获得WebSettings 调用WebSettings 的方法,配置webview WebSettings webSet ...