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

国密(3)- 预主密钥/主密钥计算和Finished消息的加解密 国密的ECC-SM3-SM4-CBC模式下的预主密钥和主密钥的生成过程介绍,以及Finished消息的加解密介绍 阅读详情

4. Median of Two Sorted Arrays

Hard

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

 

笔记:

方法1:O( (m+n)long(m+n) )

class Solution:
    def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float:
        new_nums = nums1 + nums2
        new_nums.sort()
        temp_index = (len(new_nums) + 1) // 2
        if len(new_nums) % 2 == 0:
            return (new_nums[temp_index - 1] + new_nums[temp_index]) / 2
        return new_nums[temp_index - 1]

方法2:O( (m+n) / 2)

class Solution:
    def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float:
        mid_index = (len(nums1) + len(nums2) + 1) // 2
        index = 0
        i_nums1, i_nums2 = 0, 0
        n1, n2 = 0, 0
        while index <= mid_index:
            n1 = n2
            index += 1
            if i_nums1 < len(nums1) and i_nums2 < len(nums2):
                if nums1[i_nums1] < nums2[i_nums2]:
                    n2 = nums1[i_nums1]
                    i_nums1 += 1
                else:
                    n2 = nums2[i_nums2]
                    i_nums2 += 1
            elif i_nums1 < len(nums1) and i_nums2 >= len(nums2):
                n2 = nums1[i_nums1]
                i_nums1 += 1
            elif i_nums1 >= len(nums1) and i_nums2 < len(nums2):
                n2 = nums2[i_nums2]
                i_nums2 += 1
        return (n1 + n2) / 2 if (len(nums1) + len(nums2)) % 2 == 0 else n1

方法3:O(log(m+n))

思路:

我们规定每次的nums1都是长度小于等于nums2的长度的。我们对nums1进行二分,并根据取的nums1 的数的个数来决定nums2到底取几个数。

class Solution:
    def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float:
        if len(nums1) > len(nums2):
            return self.findMedianSortedArrays(nums2, nums1)
        len_nums1, len_nums2 = len(nums1), len(nums2)
        low, high = 0, len_nums1
        while low <= high:
            partition_x = (low + high) // 2
            partition_y = (len_nums1 + len_nums2 + 1) // 2 - partition_x

            max_left_x = float('-inf') if partition_x == 0 else nums1[partition_x - 1]
            min_right_x = float('inf') if partition_x == len_nums1 else nums1[partition_x]

            max_left_y = float('-inf') if partition_y == 0 else nums2[partition_y - 1]
            min_right_y = float('inf') if partition_y == len_nums2 else nums2[partition_y]

            if max_left_x <= min_right_y and max_left_y <= min_right_x:
                if (len_nums1 + len_nums2) % 2 == 0:
                    return (max(max_left_x, max_left_y) + min(min_right_x, min_right_y)) / 2
                else:
                    return max(max_left_x, max_left_y)
            elif max_left_x > min_right_y:
                high = partition_x - 1
            else:
                low = partition_x + 1

 

重复点击提交、产生多笔数据、保持数据只操作一次---->接口幂等性校验 幂等校验一、工作真实场景 + 常出现场景二、工作解决方案 + 百度解决方案 一、工作真实场景 + 常出现场景 真实场景:在一次工作中进行成品出库创建成品出库单时,手抖了一下,重复点击了两次确定(提交表单)。结果很神奇的发现居然产生了两笔一模一样的数据(流水号都一样),当时就很懵逼,稍作思考,想想应该是在同一时刻创建了两个出库单。感觉很有意思(因为之前没有遇到过,写代码的时候也没有考虑到这个问题的发生),后面换了一种方式复现场景:提交表单时多次点击Enter按钮,还是会产生多笔重复数据。 常见场景: 订单接 阅读详情

相关推荐

工业视觉中HDR算法的动态范围优化与Debevec/Mertens方法实践

本文深入探讨了工业视觉中HDR(高动态范围)成像技术,重点解析了Debevec和Mertens两种核心算法。针对工业场景中因动态范围不足导致的过曝与欠曝问题,文章详细对比了两种方法的原理、实现步骤、参数调优及适用场景,为工程师在静态检测与高速产线等不同需求下,选择并实践HDR算法提供了清晰的指导。

weixin_29099217的博客 198

【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的博客 647

VBOX 3iS实战:如何用GNSS+IMU搞定车辆动态坡度测量(附避坑指南)

本文详细介绍了VBOX 3iS在车辆动态坡度测量中的高阶应用,通过GNSS+IMU融合技术和卡尔曼滤波算法,有效解决了传统测量方法在动态环境中的局限性。文章提供了设备配置、实战策略和数据后处理的实用指南,帮助工程师提升测量精度和效率。

weixin_42520796的博客 325

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

一只奋斗的小虫的博客 533

用友U8库龄分析避坑指南:物料出入库日期计算的5个常见错误

本文深入剖析了在用友U8系统中进行库龄分析时,物料出入库日期计算的五个常见陷阱。从数据源选择、业务类型过滤、计价方式逻辑、期初数据处理到查询性能优化,逐一解析错误根源并提供避坑指南,帮助企业获得准确的库龄数据,有效管理库存与资金风险。

weixin_29209801的博客 462

利用易特天下 ipbox&零层壹号 sdwan 组网盒子实现异地组网

一款支持zerotier的异地组网盒子

v808v808的博客 3367

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的博客 1432

CGAL 网格等高线计算

CGAL 网格等高线计算

dayuhaitang1的博客 460

【leetcode】两个排序数组中的中位数(Median of Two Sorted Arrays)【python】

需要注意两点 可能存在一个数组遍历完毕的情况,这时候只需要遍历另外一个数组 数组可能为空,必须事先进行判断,否则会越界 算法思想 一次遍历两个数组,将较小的加入到新数组sums中,这样sums就是有序的,跟合并两个有序的链表的思想一样 class Solution: def findMedianSortedArrays(self, nums1, nums2): ...

fanc的博客 1084

西门子SMART PLC增量式PID(完整梯形图FC)

西门子PLC自带的PID指令是基于位置型的PID,位置型PID的算法公式和详细介绍,大家可以参看《PID专栏》的其它文章,这里不再赘述。本篇博文直接给出代码部分的讲解,不设计太多公式和理论的推导。限于本人能力,文中难免出现错误和不足之处,诚恳欢迎大家的批评和指正。同时感谢大家的订阅和关注。SMART200 PLC系统自带的PID指令解析,可以参看下面这篇文章:SMART PLC PID算法基本解析(附公式)_RXXW_Dor的博客-CSDN博客_plc的pid算法在态运行中,控制器调节输出值,使偏差 (e

RXXW_Dor的博客 8158

王者荣耀AI绝悟如何选英雄?

王者荣耀AI绝悟如何选英雄? 【抢射手?抢中单位?「绝悟」在打王者荣耀时是如何选英雄的?】 腾讯 AI Lab 开发的 AI 智能体「绝悟」已让王者峡谷不再只是人类召唤师的竞技场,而且这个 AI 战队在上月底进化成了「完全体」。在一局完整的比赛中,英雄选择阶段是至关重要的(比如五射手或五法师阵容会有官方劝退)。 近日,腾讯 AI Lab 和上海交通大学发布的一篇论文介绍了绝悟的英雄选择策略:Ju...

不断学习,不断进步,提高自己 1万+

【leetcode】4. Median of Two Sorted Arrays(Python & C++)

4. Median of Two Sorted Arrays题目链接3.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

I am what i am 1004

2023年高教社杯 | 2023年高教社杯全国大学生数学建模思路(A题思路、C题思路)

2023年高教社杯 | 2023年高教社杯全国大学生数学建模思路(A题思路、C题思路)

前程算法屋的博客 626

LeetCode 4. Median of Two Sorted Arrays Python3

LeetCode 4. Median of Two Sorted Arrays Python3 Description 点击查看题目 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 t...

与你的博客 419

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

[LeetCode]4. Median of Two Sorted Arrays

每天一道编程题题目描述样例python解法C语言解法 题目描述 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...

xingyu97的博客 147

全球国家地区城市数据

MySQL全球国家地区城市数据报。。。。。。。。。。。。

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

[LeetCode] 4. Median of Two Sorted Arrays(Python)1. 题目2. 题目理解3. 代码实现 1. 题目 Given two sorted arrays nums1 and nums2 of size m and n respectively, return the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)). Example 1:

qq_35029666的博客 933

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

Median of Two Sorted Arrays

LeetCode上的一道题目,有点意思,求两有序数组放一块以后的中位数,复杂度要求O(log(n+m)) 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 complexi

XiaoPANGXia的专栏 867

[LeetCode] 004. Median of Two Sorted Arrays (Hard) (C++/Java/Python)

[LeetCode] 004. Median of Two Sorted Arrays (Hard) (C++/Java/Python)

水果君の日常 6456

4. Median of Two Sorted Arrays

题目 Given two sorted arrays nums1 and nums2 of size m and n respectively. Return the median of the two sorted arrays. Follow up: The overall run time complexity should be O(log (m+n)). 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/median-of-two-sorted

努力努力再努力的小马 165

Leetcode-04-Median of Two Sorted Arrays-python

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

goffxing的博客 342

LeetCode:4. Median of Two Sorted Arrays

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

Wisimer 127

LeetCode 4 Median of Two Sorted Arrays 解题报告

原文链接: http://hankerzheng.com/blog/LeetCode-Median-of-Two-Sorted-ArraysProblem DescriptionLeetCode 4 Median of Two Sorted Arrays There are two sorted arrays nums1 and nums2 of size m and n respectively

hanzheng992的博客 435

LeetCode 4.Median of Two Sorted Arrays (Python)

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

iwanthn的博客 515

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:

Algorithmguy的专栏 509

LeetCode-4 Median of Two Sorted Arrays (2)

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

秦智慧的博客 124

LeetCode Median of Two Sorted Arrays

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

DRFish 110
上一篇: [LeeCode] 733. Flood Fill Easy (Python)
下一篇: [LeeCode] 33. Search in Rotated Sorted Array (Python)
Haskei
博客等级 码龄10年 29粉丝 281原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值