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. stylus(css预编译器)

    推荐去张鑫旭大神这里详细了解:http://www.zhangxinxu.com/jq/stylus/ 安装 npm install -g stylus 自动编译 $ stylus -w demo.s ...

  2. github常见操作和常见错误

    配置git的时候会使用git config,三种配置分别为git config.git config --global.git config --system. 它们之前的优先级为(由高到低):git ...

  3. LeetCode 中级 - 重新排序得到的幂(105)

    从正整数 N 开始,我们按任何顺序(包括原始顺序)将数字重新排序,注意其前导数字不能为零. 如果我们可以通过上述方式得到 2 的幂,返回 true:否则,返回 false. 示例 1: 输入:1 输出 ...

  4. chromium之MessagePump.h

    上代码,注释已经写得很详细了. 粗看一下,这是个纯虚类,用于跨平台的通用接口. MessagePump,Pump的意思是泵,,MessagePump也就是消息泵,输送消息 namespace base ...

  5. Excel2013打开提示 文件格式和扩展名不匹配。文件可能已损坏或不安全。除非您信任其来源,否则请勿打开。是否仍要打开它?

    有的时候打开xls文档时,会提示“文件格式和扩展名不匹配.文件可能已损坏或不安全.除非您信任其来源,否则请勿打开.是否仍要打开它?” 遇到这种情况,我们需要 1.win键+R键,打开“运行“,输入re ...

  6. es6-----部分新特性详解

    ES6(ECMAScript 6)是即将到来的新版本JavaScript语言的标准,代号harmony(和谐之意,显然没有跟上我国的步伐,我们已经进入中国梦版本了).上一次标准的制订还是2009年出台 ...

  7. ziplist之详细分析

    压缩列表ziplist ziplist是一种连续,无序的数据结构.压缩列表是 Redis 为了节约内存而开发的, 由一系列特殊编码的连续内存块组成的顺序型(sequential)数据结构. 组成 属性 ...

  8. SSH Secure :Algorithm negotiation failed,反复提示输入password对话框

    在嵌入式开发中,SSH Secure File Transfer Client 软件使用,方便了windows和linux之间文件拷贝,尤其是多台主机状况下. 最近装了Ubuntu 16.0.4,在V ...

  9. ruby 字符串加密

    str = 'This is a test.rb!' #DES加密 puts str.crypt('salt') #MD532位加密 require 'digest' puts Digest::MD5 ...

  10. java中有关Volatile的几个小题

    1.java中能创建volatile数组吗? 能,java中可以创建volatile数组,不过只是一个指向数组的引用,而不是整个数组,如果改变引用指向的数组,将会受到volatile的保护,但是如果多 ...