给定两个大小为 m 和 n 的有序数组 nums1 和 nums2 。

请找出这两个有序数组的中位数。要求算法的时间复杂度为 O(log (m+n)) 。

你可以假设 nums1 和 nums2 不同时为空。

示例 1:

nums1 = [1, 3]
nums2 = [2]

中位数是 2.0

示例 2:

nums1 = [1, 2]
nums2 = [3, 4]

中位数是 (2 + 3)/2 = 2.5
class Solution:
    def findMedianSortedArrays(self, nums1, nums2):
        """
        :type nums1: List[int]
        :type nums2: List[int]
        :rtype: float
        """
        nums = nums1 + nums2
        nums.sort()
        l = len(nums)
        if l % 2 == 0:
            return (nums[int(l/2)] + nums[int(l/2-1)])/ 2
        else:
            return nums[int((l-1)/2)]

if __name__ == '__main__':
    s = Solution()
    print(s.findMedianSortedArrays([1,3],[2]))
    print(s.findMedianSortedArrays([1,2],[3,4]))

leetcode python两个排序数组的中位数的更多相关文章

  1. LeetCode 4 - 两个排序数组的中位数 - [分治]

    题目链接:https://leetcode-cn.com/problems/median-of-two-sorted-arrays/description/ 给定两个大小为 m 和 n 的有序数组 n ...

  2. LeetCode#5 两个排序数组的中位数

      给定两个大小为 m 和 n 的有序数组 nums1 和 nums2 . 请找出这两个有序数组的中位数.要求算法的时间复杂度为 O(log (m+n)) . 你可以假设 nums1 和 nums2  ...

  3. leetcode 4.两个排序数组的中位数

    题目: 给定两个大小为 m 和 n 的有序数组 nums1 和 nums2 . 请找出这两个有序数组的中位数.要求算法的时间复杂度为 O(log (m+n)) . 你可以假设 nums1 和 nums ...

  4. leetcode,两个排序数组的中位数

    先上题目描述: 给定两个大小为 m 和 n 的有序数组 nums1 和 nums2 . 请找出这两个有序数组的中位数.要求算法的时间复杂度为 O(log (m+n)) . 你可以假设 nums1 和  ...

  5. LeetCode(4):两个排序数组的中位数

    Hard! 题目描述: 有两个大小为 m 和 n 的排序数组 nums1 和 nums2 . 请找出两个排序数组的中位数并且总的运行时间复杂度为 O(log (m+n)) . 示例 1: nums1 ...

  6. LeetCode4. 两个排序数组的中位数

    4. 两个排序数组的中位数 问题描述 There are two sorted arrays nums1 and nums2 of size m and n respectively.Find the ...

  7. 2.Median of Two Sorted Arrays (两个排序数组的中位数)

    要求:Median of Two Sorted Arrays (求两个排序数组的中位数) 分析:1. 两个数组含有的数字总数为偶数或奇数两种情况.2. 有数组可能为空. 解决方法: 1.排序法 时间复 ...

  8. LeetCode-4. 两个排序数组的中位数(详解)

    链接:https://leetcode-cn.com/problems/median-of-two-sorted-arrays/description/ 有两个大小为 m 和 n 的排序数组 nums ...

  9. JavaScript实现获取两个排序数组的中位数算法示例

    本文实例讲述了JavaScript排序代码实现获取两个排序数组的中位数算法.分享给大家供大家参考,具体如下: 题目 给定两个大小为 m 和 n 的有序数组 nums1 和 nums2 . 请找出这两个 ...

随机推荐

  1. 小白月赛13 小A的路径 (矩阵快速幂求距离为k的路径数)

    链接:https://ac.nowcoder.com/acm/contest/549/E来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言52428 ...

  2. dedecms安装操作-重安装-开启GD库

    wamp5_1.7.4 打开:http://localhost/install/安装 (输入:域名/install,回车跳转到安装界面,勾选“我已经阅读并同意此协议”,然后点击“继续按钮”.) 重安装 ...

  3. Linux uniq 命令

    Linux uniq 命令  Linux 命令大全 Linux uniq 命令用于检查及删除文本文件中重复出现的行列,一般与 sort 命令结合使用. uniq 可检查文本文件中重复出现的行列. 语法 ...

  4. <HTML>初识HTML

    最近在阅读Head first HTML and CSS, 写一些笔记.   小知识: 1. 浏览器会忽略HTML文档中的制表符,回车和大部分空格——要用标记 2. WYSIWYG——使得用户在视图中 ...

  5. Dynamic Clock in Terminal.

    #!/bin/bash tput civis while [ 1 ] do tput clear # tput cup 10 20 info=$(date "+%Y-%m-%d %H:%M: ...

  6. Python——使用高德API获取指定城指定类别POI并实现XLSX文件合并

    # 以下内容为原创,转载请注明出处 1 import xlrd # 读xlsx import xlsxwriter # 写xlsx import urllib.request # url请求,Pyth ...

  7. JS中JSON和string字符串相互转换

    在Firefox,chrome,opera,safari,ie9,ie8等高级浏览器直接可以用JSON对象的stringify()和parse()方法. JSON.stringify(obj)将JSO ...

  8. Hibernate 分页

    public EmailBean[] findByPage(Date begin, Date end, String emailreference, int pageNo, final int pag ...

  9. P3396 哈希冲突

    很好的根号算法(这种思想好像叫根号分治?) 首先,暴力是Ο(n2)的 考虑预处理: for(p=1;p<=n;p++) //枚举模数 ans[p][i%p]+=value[i]; 看似很好但还是 ...

  10. 使用Hexo+github搭建个人博客

    目录 创建Github仓库 环境安装 安装Node.js 安装Git 检查安装 安装Hexo 连接Hexo和Github 设置Git的用户名和邮箱 配置SSH 配置Deployment 新建第一篇博客 ...