题目描述:

中文:

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

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

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

英文:

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)).

You may assume nums1 and nums2 cannot be both empty.

class Solution(object):
def findMedianSortedArrays(self, nums1, nums2):
"""
:type nums1: List[int]
:type nums2: List[int]
:rtype: float
"""
n1,n2 = len(nums1), len(nums2)
if n1 > n2:
nums1, nums2, n1, n2 = nums2, nums1, n2, n1 imin, imax, half_len = 0, n1, (n1 + n2 + 1) // 2
while imin <= imax:
i = (imin + imax) // 2
j = half_len - i
if j > 0 and i < n1 and nums1[i] < nums2[j-1]:
imin = i + 1
elif i > 0 and j < n2 and nums1[i-1] > nums2[j]:
imax = i - 1
else: if i == 0: max_of_left = nums2[j-1]
elif j == 0: max_of_left = nums1[i-1]
else: max_of_left = max(nums1[i-1], nums2[j-1]) if (n1 + n2) % 2 == 1:
return max_of_left if i == n1: min_of_right = nums2[j]
elif j == n2: min_of_right = nums1[i]
else: min_of_right = min(nums1[i], nums2[j]) return (max_of_left + min_of_right) / 2.0

题目来源:力扣

力扣 -- 寻找两个有序数组的中位数 Median of Two Sorted Arrays python实现的更多相关文章

  1. 《LeetCode-0004》 寻找两个有序数组的中位数-Median of Two Sorted Arrays

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

  2. [Swift]LeetCode4. 两个排序数组的中位数 | 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 ...

  3. Java实现 LeetCode 4 寻找两个有序数组的中位数

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

  4. LeetCode Golang 4. 寻找两个有序数组的中位数

    4. 寻找两个有序数组的中位数 很明显我偷了懒, 没有给出正确的算法,因为官方的解法需要时间仔细看一下... func findMedianSortedArrays(nums1 []int, nums ...

  5. 0004. 寻找两个有序数组的中位数(Java)

    4. 寻找两个有序数组的中位数 https://leetcode-cn.com/problems/median-of-two-sorted-arrays/ 最简单的就是用最简单的,把两个数组分别抽出然 ...

  6. Leetcode(4)寻找两个有序数组的中位数

    Leetcode(4)寻找两个有序数组的中位数 [题目表述]: 给定两个大小为 m 和 n 的有序数组 nums1 和* nums2. 请你找出这两个有序数组的中位数,并且要求算法的时间复杂度为 O( ...

  7. 【LeetCode】寻找两个有序数组的中位数【性质分析+二分】

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

  8. leetcode -- 寻找两个有序数组的中位数

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

  9. PHP算法之寻找两个有序数组的中位数

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

随机推荐

  1. AbstractQueuedSynchronizer简单使用

    AQS是JUC中很多同步组件的构建基础,简单来讲,它内部实现主要是状态变量state和一个FIFO队列来完成,同步队列的头结点是当前获取到同步状态的结点,获取同步状态state失败的线程,会被构造成一 ...

  2. SpringBoot集成H2database

    转载:https://blog.csdn.net/chenhao_c_h/article/details/80332260 h2database为我们提供了十分轻量,十分快捷方便的内嵌式数据库 H2是 ...

  3. sentinel集群docker-compose.yml配置

    redis安装 version: '3.1'services: master: image: redis container_name: redis-master ports: - 6379:6379 ...

  4. 【leetcode】1013. Pairs of Songs With Total Durations Divisible by 60

    题目如下: In a list of songs, the i-th song has a duration of time[i] seconds. Return the number of pair ...

  5. Centos6安装zabbix-agent

    一.安装yum源 rpm -ivh https://repo.zabbix.com/zabbix/3.4/rhel/6/x86_64/zabbix-release-3.4-1.el6.noarch.r ...

  6. sql查看数据库环境及一些参数

    sql查看数据库环境及一些参数 select parent_obj from sysobjects where name='FK_Student_banjiID' --根据外键名得到外表id sele ...

  7. 网络体系之TCP/IP模型

    TCP/IP参考模型是因特网使用的参考模型,这个体系结构在它的两个主要协议出现以后,被称为TCP/IP参考模型.该模型将网络协议分为四层:网络接口层.网络层.运输层.应用层. TCP/IP协议不是TC ...

  8. Orcad Capture元件库介绍--Cadence Allegro

    绘制原理图和PCB,最好有自己的元件封装.元件库 ORCAD CAPTURE元件库介绍 AMPLIFIER.OLB amplifier 共182个零件,存放模拟放大器IC,如CA3280,TL027C ...

  9. pytest_用例运行级别_class级

    ''' 模块级(setup_module/teardown_module)开始于模块始末, 全局的在类中不起作用 类级(setup_class/teardown_class)只在类中前后运行一次(在 ...

  10. Linux 下 VIM 的操作

    其实VI 和vim本质上没有多大区别,,但是VIM 可以高亮关键字,使得更受青睐 vim里面有3种模式:命令模式,编辑模式,末行模式 1. vim--->:set number VIM 打开文档 ...