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. 【题解】洛谷P1074 [NOIP2009TG] 靶形数独(DFS+剪枝)

    洛谷P1074:https://www.luogu.org/problemnew/show/P1074 思路 这道题一看就是DFS 打一个分数表方便后面算分 我用x y z数组分别表示行 列 宫 是否 ...

  2. Java添加事件的几种方式(转载了codebrother的文章)

    /** * Java事件监听处理——自身类实现ActionListener接口,作为事件监听器 * * @author codebrother */ class EventListener1 exte ...

  3. Unity透明视频播放 所需的Shader脚本

    Shader "Custom/ShaderMovie" { Properties { _MainTex("Color (RGB)", 2D) = "b ...

  4. 【浏览器-Safari-网页开发指南】官方Sarari开发指南译文

    Safari是一个功能比较完整的浏览器.它支持所有按照W3C标准HTML,CSS,Javascript开发的网页. 当然,让网站正常访问只是我们的初级目标.开发者应该致力于给用户提供更好的用户体验.比 ...

  5. mysql导出数据很快,导入很慢

    mysql导出快,导入特别慢的解决方法:在导入时添加两个参数:max_allowed_packet; net_buffer_length --max_allowed_packet   客户端/服务器之 ...

  6. Servlet Struts2 SpringMVC 获取参数与导出数据 方法比较

    servlet中通过request.getParameter()从网页获取参数 通过request session servletContext几个域对象的setAttribute(String ,O ...

  7. IDEA(2018.3.2)

    N757JE0KCT-eyJsaWNlbnNlSWQiOiJONzU3SkUwS0NUIiwibGljZW5zZWVOYW1lIjoid3UgYW5qdW4iLCJhc3NpZ25lZU5hbWUiO ...

  8. collections.ChainMap类合并字典或映射

    ## 使用update()方法或者ChainMap类合并字典或映射 # 使用update()方法合并 a = {'x': 1, 'z': 3} b = {'y': 2, 'z': 4} merged ...

  9. python生成器函数中return的作用

    当生成器函数中含有return时,return不会返回任何值,会直接终止当前生成器,对yield的作用没有影响,当函数执行到return时候,调用next()来执行生成器则会报错,如果使用for循环遍 ...

  10. 常量的三种定义方式和static在c语言中的三种修饰

    c语言的常量在执行期间为固定值,在定义后无法被修改常量可以是任何的数据基本类型,可以为整形,浮点常量,字符和字符串常量1,使用const关键字2, 使用宏定义3,使用枚举常量 枚举: #include ...