Leetcode-04-Median of Two Sorted Arrays-python

【信息科学与工程学】【数据科学】数据科学领域 第六篇 算法设计 07 堆、栈、表、矩阵、集合、队列、数组、字符串、树、图、动态规划、结构体、元组的算法01 当前段必须包含所有字符的最后出现位置。1. 字符串分割成最多片段2. 基因序列功能域划分3. 文本段落划分4. 音频声道分离5. 图像色彩分区6. 社交网络社群划分7. 蛋白质结构域8. 音乐乐章分段9. 代码模块划分10. 章节划分特征:一次扫描,贪心确定边界。 阅读详情

1.1 题目

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.

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

The median is 2.0
Example 2: nums1 = [1, 2] nums2 = [3, 4]

The median is (2 + 3)/2 = 2.5

题目的思考:
两个数组中的中位数,并且要求在于log(m+n),想到这个的话便想到了二分查找,这是目前通过题目得出信息,分析具体,要找到具体的中位数,所以需要将两个数组合并,然后在进行排序,中位数的特殊位置,查找到中位数,利用python自带的一些方法进行操作。

1.2 代码

class Solution(object):
    def findMedianSortedArrays(self, nums1, nums2):
        """
        :type nums1: List[int]
        :type nums2: List[int]
        :rtype: float
        """
        if len(nums1) < 1 and len(nums2) <1:
            return False
        nums1.extend(nums2)
        nums1.sort()
        
        mid = len(nums1) // 2
        if len(nums1) % 2  == 0:
            return (nums1[mid]+nums1[mid-1])/2.0
        else:
            return nums1[mid]

1.3 对结果的分析

通过提交得出下面的结果
在这里插入图片描述
发现这种方式还是属于暴利的方式,而且是通过python的一些封装好的方式去解决,然后查询了leetcode的和其他的博主的方法,了解到了解题的思路:就是利用题目中已经阐述了两组数组都是有序,然后从两组数组的中间位置的数提取出来进行比较,然后中间数大的那一组往递减的方向移动,数小的那一组往递增的方向移动,直到发生两组数比较与前面的比较发生了转换的时候,便说明中位数在这四个数中间的位置。举例分析
在这里插入图片描述
在这里插入图片描述
备注:图片来自相关链接。

相关代码

def median(A, B):
    m, n = len(A), len(B)
    if m > n:
        A, B, m, n = B, A, n, m
    if n == 0:
        raise ValueError

    imin, imax, half_len = 0, m, (m + n + 1) / 2
    while imin <= imax:
        i = (imin + imax) / 2
        j = half_len - i
        if i < m and B[j-1] > A[i]:
            # i is too small, must increase it
            imin = i + 1
        elif i > 0 and A[i-1] > B[j]:
            # i is too big, must decrease it
            imax = i - 1
        else:
            # i is perfect

            if i == 0: max_of_left = B[j-1]
            elif j == 0: max_of_left = A[i-1]
            else: max_of_left = max(A[i-1], B[j-1])

            if (m + n) % 2 == 1:
                return max_of_left

            if i == m: min_of_right = B[j]
            elif j == n: min_of_right = A[i]
            else: min_of_right = min(A[i], B[j])

            return (max_of_left + min_of_right) / 2.0

1.4 sort

其实就是系统内部实现了一种综合的排序方法,只是封装好直接用,看似所写的代码短,其实在执行时是进行了排序的,造成时间复杂度的增加,而后面的方法是直接通过递归来实现寻找中位数的方法,极大的减缓了时间复杂度。

1.5 相关链接

1.LeetCode 004 Median of Two Sorted Arrays 详细分析(图片来源)
2.C++,java,Python的内部实现sort怎么实现的,有什么不同?
3.leetcode官方答案
4.python sort函数内部实现原理

低代码Agent开发框架使用指南(二)—Coze平台核心功能概览 本篇分享系统介绍了字节跳动推出的低代码智能体开发平台Coze。Coze以免费易用、多模型支持和便捷的跨平台发布为核心优势,显著降低了智能体开发门槛。文章详细解析了平台的六大核心功能:扩展能力边界的插件系统、增强认知的知识库、结构化数据管理的数据库、可视化图像处理的图像流、实现复杂逻辑编排的工作流,以及处理复杂任务的多智能体协作机制,全面展现了Coze平台的强大能力。 阅读详情

相关推荐

Window系统下搭建GIT本地服务器

这是一篇在window下搭建git本地服务器的教程,带你一步步搭建出本地的git服务器。

qwer971211的博客 2万+

【leetcode-Python】- 二分搜索 - 4. Median of Two Sorted Arrays

题目链接 4. Median of Two Sorted Arrays 题目描述 给定两个非降序数组nums1和nums2,大小分别为m和n,返回两个数组合并后的中位数。要求时间复杂度为O(log(m+n)). 示例 输入:nums1 = [1,3],nums2 = [2] 输出:2.00000 输入:nums1 = [1,2],nums2 = [3,4] 输出:2.50000 输入:nums1= [],nums2 = [1] 输出:1.0000 解决思路一 插一句题外话

gulaixiangjuejue的博客 646

深度学习的unfold操作

对于彩色图像这样的三维数据,unfold操作会对每个通道分别进行上述的二维展开操作。例如,一个 RGB 图像,在对其进行unfold操作时,红色通道、绿色通道和蓝色通道的数据都会被独立地按照定义的窗口和步长展开,然后将这些展开后的通道数据按照原来的通道顺序组合在一起,形成新的张量。与我们熟悉的卷积类似,unfold也是使用一个特定大小的窗口和步长自左至右、自上至下滑动,不同的是,卷是滑动后与核求乘积(所以取名为卷积),而unfold滑动后不求乘积,是将窗口内的数据展开。

xulibo5828的博客 2167

【python】【leetcode】【算法题目4—Median of Two Sorted Arrays】

一、题目描述 题目原文: There are two sorted arraysnums1andnums2of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).

一只奋斗的小虫的博客 532

Windows7 64位系统PL2303驱动无法安装问题解决

问题描述:电脑安装的是Win7 64位的旗舰版,USB to RS232这种线很简单,对于笔记本如果要用串口,就不能少了它了。USB to RS232转换芯片种类繁多,常见的有ch340,0108,PL2303,FT232等,这里面FT232的应该价格是相对比较贵的,因为FTDI的芯片本身比较贵。由于买了一根PL2303芯片的USB to RS232转换线,于是插上后发现驱动无法安装,又是在设备管

suxiang198的博客 5809

iTunes登录协议逆向与自动化实现:从抓包到3DES加密的完整实战

网络协议逆向与自动化是提升开发与测试效率的关键技术,其核心在于理解客户端与服务器之间的通信原理。通过分析HTTPS请求、逆向加密算法,可以模拟合法客户端行为,实现批量操作自动化,在自动化测试与账号管理等场景中具有重要价值。本文聚焦于iTunes登录协议,深入剖析了其认证流程,并利用Frida动态调试工具成功逆向出关键的3DES加密环节,为开发者提供了一套从协议分析到Python代码实现的完整解决方案。

480

leetcode算法训练_04

给你一个由 n 个整数组成的数组 nums ,和一个目标值 target。你可以按 任意顺序 返回答案。示例 1:输入:nums = [1,0,-1,0,-2,2], target = 0输出:[[-2,-1,1,2],[-2,0,0,2],[-1,0,0,1]]示例 2:输入:nums = [2,2,2,2,2], target = 8输出:[[2,2,2,2]]

2302_80932253的博客 232

陀螺仪及四元数解算

陀螺仪 四元数

zb313982521的博客 2005

LeetCode in Python 4. Median of Two Sorted Arrays (寻找两个正序数组的中位数)

重复直至找到一个合适的m1使得从两个数组中取出的元素为合并两数组后最小的half个元素。4)对于数组奇偶元素个数的分开判断,如果数组有奇数个元素,则中位数应该是取完half(此half为向下取的整数)后两个数组右中位数的最小值,举例说明,有13个元素,half=6,中位数应为两数组合并后第7个元素,而第7个元素来源只能是取完half个数后两数组的右中位数中的一个,即right1和right2中的一个,根据正序排列的顺序,小的在前大的在后,故第7个元素应为min(right1, right2)。

m0_45175452的博客 1427

企业级托管钱包架构设计与MPC密钥管理:基于Go语言的生产级实践

本文探讨了2026年Web3生态中企业级托管钱包的技术架构与实现方案。文章指出,传统私钥管理模式已无法满足机构级资产管理需求,而基于多方安全计算(MPC)和门限签名(TSS)的技术方案成为主流选择。研究分析了托管钱包的四层架构设计,详细解读了MPC的核心技术原理,包括分布式密钥生成(DKG)和签名协议演进。通过Go语言实现案例,展示了密钥分片管理、DKG流程和交易签名等核心模块的代码实现。文章还提出了生产部署的安全原则、容灾设计和监控体系,并讨论了密钥撤销等现存挑战及TEE融合等未来趋势。研究为构建安全、高

分享AI、区块链技术及应用,未来已来 81

第八届传智杯微信小程序开发挑战赛练习题库

本文是微信小程序开发练习题库,包含单选题、多选题和判断题三种题型。单选题主要考察小程序开发基础知识,如数组方法、组件使用、API调用等;多选题涵盖样式属性、页面跳转、数据处理等综合知识;判断题则测试开发者对小程序特性、生命周期、功能限制等概念的理解。题目内容涉及小程序开发的各个方面,包括组件使用、API调用、数据绑定、页面配置等核心知识点,适合开发者进行自我测试和知识巩固。

qq_73044452的博客 506

[LeeCode] 4. Median of Two Sorted Arrays (Python)

4.Median of Two Sorted Arrays Hard There are two sorted arraysnums1andnums2of 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 assumenums1andnums2cannot be bot...

热爱编程,热爱技术 241

《点云进阶》专栏文章目录

点云进阶》专栏文章目录

点云侠的博客 4905

leetcode04-Median of Two Sorted Arrays-python

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)). 找两个序列的中位数。 想当然的思路将两个序列合二为一,

summerdj的博客 1855

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

win10 跳过U8环境检测的IE WebControls.rar

win10 安装u8的时候 提示IE WebControls 不通过,这个工具可以欺骗u8的检查。 跳过U8环境检测的IE WebControls。提示导入失败后,重新检测通过。

(python)leetcode刷题笔记04 Median of Two Sorted Arrays

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

weixin_30340775的博客 80

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

【LeetCode】4. Median of Two Sorted Arrays解题报告(Python)

参考博客 测试代码: class Solution: def getKth(self, A, B, k): k = int(k) lenA = len(A); lenB = len(B) if lenA &amp;amp;amp;gt; lenB: return self.getKth(B, A, k) if lenA == 0: r...

L141210113的专栏 1215

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

两个有序数组的中位数Median of Two Sorted Arrays(很重要)

https://leetcode.com/problems/median-of-two-sorted-arrays/ 对于一个长度为n的已排序数列a,若n为奇数,中位数为a[n / 2 + 1] , 若n为偶数,则中位数(a[n / 2] + a[n / 2 + 1]) / 2 如果我们可以在两个数列中求出第K小的元素,便可以解决该问题 不妨设数列A元素个数

知之可否 8544

两个有序数组的中间值(Median of Two Sorted Arrays)__By leetCode

Example nums1 = [1, 3] nums2 = [2] The median is 2.0 nums1 = [1, 2] nums2 = [3, 4] The median is (2 + 3)/2 = 2.5 求两个有序数组的中间值 One public double findMedianSortedArrays(int[] a, int...

huisiwarmhome的博客 977

leetcode之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:

漫漫学习路 2949

leetcode 4(python实现)

leetcode 4 题目描述 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 assu...

qq_43271202的博客 861

python写算法题:leetcode: 4. Median of Two Sorted Arrays

https://leetcode.com/problems/median-of-two-sorted-arrays/#/description class Solution(object): def findPos(this,n1,pos1,n2,pos2,pos): limit1=len(n1) limit2=len(n2) p=0

326

4. Median of Two Sorted Arrays两个有1序数组的中位数Python

有两个分别为大小m和n的排序数组nums1和nums2。 找到两个排序数组的中位数。总体运行时复杂度应为O(log(n+m))。 假设nums1和nums2不能都为空。 nums1 nums2 Median A [2] [] 2 B [3] [-2,-1] -1 C [1,3,4] [-2,5,6] 3.5 题中的中位数是相当于把nums1和nums2合并之后从小到大排序中间的数, 不考虑时间复杂度可以用sor..

weixin_44516745的博客 220

[Leetcode]4. Median of Two Sorted Arrays @python

题目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)).题目要求有两个排好序的数组nums1,nums2,找到这

qian2729的专栏 542

Leetcode4答案与解析 - Median of Two Sorted Arrays(python)

原题 Leetcode原题传送门 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...

liubai01的博客 526

LeetCode Median of Two Sorted Arrays 在两个已排列的数组中找出中位数。时间复杂度为O(log(min(N,M))

完成一道题,真的是好开心,虽然自己蠢蠢的,到了研究生还在弄这些基础的算法,才开始学去整理自己的知识。但是没关系,就算是要用几年的时间才能获得别人现在就拥有的知识能力,能就用几年的时间吧!题目:There are two sorted arrays nums1 and nums2 of size m and n respectively.Find the median of the two sorte

GO THE DISTANCE ! 1万+

leetcode之 median of two sorted arrays

这是我做的第二个leetcode题目,一开始以为和第一个一样很简单,但是做的过程中才发现这个题目非常难,给人一种“刚上战场就踩上地雷挂掉了”的感觉。后来搜了一下leetcode的难度分布表(leetcode难度及面试频率)才发现,该问题是难度为5的问题,真是小看了它!网上搜了很多答案,但是鲜见简明正确的解答,唯有一种寻找第k小值的方法非常好,在此整理一下。        首先对leetcode的

姚光超的专栏 9万+
上一篇: Leetcode-01-Two Sum-python
下一篇: Leetcode-11-Container With Most Water-python
十四嵎谷
博客等级 码龄9年 2粉丝 7原创
评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值