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 = [, ]
nums2 = [] The median is 2.0

Example 2:

     nums1 = [, ]
nums2 = [, ] The median is ( + )/ = 2.5
 class Solution:
def findMedianSortedArrays(self, nums1, nums2):
"""
:type nums1: List[int]
:type nums2: List[int]
:rtype: float
"""
nums1.extend(nums2)
nums1.sort()
len1=len(nums1)
if len1:
if len1%==:
return (nums1[len1//2]+nums1[len1//2-1])/2
else:
return nums1[len1//2]

Code

 def main(nums1,nums2):
nums1.extend(nums2)
nums1.sort()
len1=len(nums1)
if len1:
if len1%==:
return (nums1[len1//2]+nums1[len1//2-1])/2
else:
return nums1[len1//2]
# print(l1)
if __name__ == '__main__':
l1=[,]
l2=[]
print(main(l1,l2))

调试代码

(python)leetcode刷题笔记04 Median of Two Sorted Arrays的更多相关文章

  1. 【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 sorted ...

  2. (python)leetcode刷题笔记03 Longest Substring Without Repeating Characters

    3. Longest Substring Without Repeating Characters Given a string, find the length of the longest sub ...

  3. (python)leetcode刷题笔记05 Longest Palindromic Substring

    5. Longest Palindromic Substring Given a string s, find the longest palindromic substring in s. You ...

  4. 【leetcode刷题笔记】Remove Duplicates from Sorted List

    Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...

  5. 【leetcode刷题笔记】Remove Duplicates from Sorted List II

    Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...

  6. 【leetcode刷题笔记】Search in Rotated Sorted Array II

    Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...

  7. 【leetcode刷题笔记】Remove Duplicates from Sorted Array II

    Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...

  8. 【leetcode刷题笔记】Search in Rotated Sorted Array

    Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...

  9. 18.9.10 LeetCode刷题笔记

    本人算法还是比较菜的,因此大部分在刷基础题,高手勿喷 选择Python进行刷题,因为坑少,所以不太想用CPP: 1.买股票的最佳时期2 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. ...

随机推荐

  1. Null 值对索引排序的影响案例一则

    --原SQL 语句如下:select * from (select tmp_tb.*, ROWNUM row_id from (select wpid, customer_id, customer_n ...

  2. springsource-tool-suite插件下载

    下载地址:    https://spring.io/tools3/sts/all/ 下载页面上的 update sites archives文件

  3. 使用Letsencrypt做SSL certificate

    为什么要使用Letsencrypt做SSL certificate? 最简单直接的原因是免费.但是免费存在是否靠谱的问题,尤其是对安全要求比较高的网站,需要考虑使用letsencrypt的安全性是否符 ...

  4. python中的super怎么用?

    面向对象有这个强大特点和作用, 著名的三大特点:封装, 继承, 多态 这篇博客写的是super()的简单理解和使用 今天在读restframework的源码的时候, 发现源码中使用了super, 依以 ...

  5. 《JavaScript实用效果整理》系列分享专栏

    整理一些使用的JavaScript效果,在Web开发中遇到的比较好的动态效果,都收藏在这里,对以后的网站开发增加不少的色彩 <JavaScript实用效果整理>已整理成PDF文档,点击可直 ...

  6. 洛谷P4526 【模板】自适应辛普森法2(simpson积分)

    题目描述 计算积分 保留至小数点后5位.若积分发散,请输出"orz". 输入输出格式 输入格式: 一行,包含一个实数,为a的值 输出格式: 一行,积分值或orz 输入输出样例 输入 ...

  7. .Net 面试题 汇总(五)

    1.简述javascript中的“=.==.===”的区别? =赋值 ==比较是否一般相等 "3"==3 //会做类型的隐式转换,true ===比较是否严格相等 "3& ...

  8. 20145202马超《java》实验5

    两人一组结对编程: 参考http://www.cnblogs.com/rocedu/p/6766748.html#SECDSA 结对实现中缀表达式转后缀表达式的功能 MyBC.java 结对实现从上面 ...

  9. Caliburn.Micro 杰的入门教程3,事件和参数

    Caliburn.Micro 杰的入门教程1(翻译)Caliburn.Micro 杰的入门教程2 ,了解Data Binding 和 Events(翻译)Caliburn.Micro 杰的入门教程3, ...

  10. 北京Uber优步司机奖励政策(1月26日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...