题目描述:

中文:

给定两个大小为 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. day18 python模块 random time sys os模块

    day18 python   一.random模块     取随机整数 import random print(random.randint(1,2))                 #顾头顾尾 p ...

  2. vue新建项目之饿了么组件标准配置

    main.js import Vue from 'vue' import App from './App.vue' import ElementUI from 'element-ui'; import ...

  3. linux清理缓存

    在free -h中查看自己的内存发现在运行一段时间后,有很多内存都被缓存占用, 所以要清理下缓存,增大可用内存 直接进入root账户,输入以下命令 echo 3 > /proc/sys/vm/d ...

  4. 谷歌使用navigator.mediaDevices.getUserMedia 调用摄像头拍照功能,不兼容IE

    <template>     <div>       <!--canvas截取流-->       <canvas ref="canvas" ...

  5. 跨域AJAX

    本篇主要讨论JSONP和CORS这两种技术,使用它们的原因是为了完成对资源的跨域访问,也就是如何绕过浏览器的同源策略Same-origin Policy. 那么什么是Same-origin Polic ...

  6. 【Dart学习】--之Iterable相关方法总结

    一,概述 按顺序访问的值或元素的集合, List集合也是继承于Iterable List和Set也是Iterable,dart:collection库中同样有很多 部分Iterable集合可以被修改 ...

  7. Android Studio使用tips

    安装位置:C:\Users\xxx\AppData\Local\Android\sdk https://developer.android.com/topic/libraries/support-li ...

  8. C# 后台报错输出到日志

    1.C# 方法 /// <summary> /// 异常处理 /// </summary> /// <returns></returns> public ...

  9. IntelliJ IDEA更新maven依赖包

    问题: IntelliJ IDEA自动载入Maven依赖的功能很好用,但有时候会碰到问题,导致pom文件修改却没有触发自动重新载入的动作,此时需要手动强制更新依赖. 方法: 方法一: ①.右键单击项目 ...

  10. Nuget-Doc:Nuget 简介

    ylbtech-Nuget-Doc:Nuget 简介 1.返回顶部 1. NuGet 简介 2019/05/24 适用于任何现代开发平台的基本工具可充当一种机制,通过这种机制,开发人员可以创建.共享和 ...