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

LeetCode 第四题答案 C++ class Solution { public: double find_mean(vector<int>&nums){ int me; double result; double er=2; if(nums.size()%2==0){ me=nums.size()/2; result= (nums[me-1]+... 阅读详情

4. Median of Two Sorted Arrays

题目链接

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

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

4.2 解题思路:

  1. 思路一:首先处理特殊情况,如果其中一个数组为空,则根据长度可直接返回另一个数组的中位数。否则同时遍历两个数组,将其按照顺序合并到一个数组中,然后根据长度即可返回中位数。

4.3 C++代码:

1、思路一代码(82ms):

class Solution93_1 {
public:
    double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
        if (nums1.size() == 0 && nums2.size() == 0)
            return 0;
        if (nums1.size() == 0)
        {
            int l = nums2.size() / 2;
            if (nums2.size() % 2 == 0)
                return double(nums2[l - 1] + nums2[l]) / 2;
            else
                return double(nums2[l]);
        }
        if (nums2.size() == 0)
        {
            int l = nums1.size() / 2;
            if (nums1.size() % 2 == 0)
                return double(nums1[l - 1] + nums1[l]) / 2;
            else
                return double(nums1[l]);
        }
        int ll = (nums1.size() + nums2.size()) / 2;
        int lu = (nums1.size() + nums2.size()) % 2;
        int i = 0, j = 0;
        vector<int>a;
        while (i < nums1.size() && j < nums2.size())
        {
            if (nums1[i] <= nums2[j])
            {
                a.push_back(nums1[i]);
                i++;
            }
            else
            {
                a.push_back(nums2[j]);
                j++;
            }
        }
        while (i < nums1.size())
        {
            a.push_back(nums1[i]);
            i++;
        }
        while (j < nums2.size())
        {
            a.push_back(nums2[j]);
            j++;
        }
        if (lu == 0)
            return double(a[ll-1] + a[ll]) / 2;
        else
            return double(a[ll]);
    }
};

4.4 Python代码:

1、思路一代码(118ms):

class Solution(object):
    def findMedianSortedArrays(self, nums1, nums2):
        """
        :type nums1: List[int]
        :type nums2: List[int]
        :rtype: float
        """
        if len(nums1)==0 and len(nums2)==0:
            return 0
        if len(nums1)==0:
        l = len(nums2) / 2
        if len(nums2)% 2 == 0:
        return float(nums2[l - 1] + nums2[l]) / 2
        else:
        return float(nums2[l])          
    if len(nums2)==0:
        l = len(nums1) / 2
        if len(nums1)% 2 == 0:
        return float(nums1[l - 1] + nums1[l]) / 2
        else:
        return float(nums1[l])  
    ll = (len(nums1) + len(nums2)) / 2
    lu = (len(nums1) + len(nums2)) % 2
    i = 0
    j = 0
    a=[]
    while i<len(nums1) and j<len(nums2):
        if nums1[i] <= nums2[j]:
        a.append(nums1[i])
        i+=1
        else:
        a.append(nums2[j])
        j+=1
    while i<len(nums1) :
        a.append(nums1[i])
        i+=1        
    while j<len(nums2):
        a.append(nums2[j])
        j+=1        
    if lu == 0:
        return float(a[ll-1]+a[ll])/2
    else:
        return float(a[ll])

[ LeetCode ] 4. Median of Two Sorted Arrays (C++ & Python) 题目: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(log ... 阅读详情

相关推荐

从稳压管到LDO芯片:一张图看懂线性电源的‘内卷’进化史

本文追溯了从稳压管到LDO芯片的线性电源技术进化历程,详细解析了各阶段的技术突破与局限。重点探讨了现代LDO在超低功耗、高精度控制等方面的创新,并提供了实战选型的黄金法则,帮助工程师在电子设备小型化与低功耗化趋势中做出最优选择。

weixin_42525551的博客 214

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

有两个有序数组nums1和nums2分别为m和n。 找到两个有序数组的中位数。 总运行时间复杂度应为O(log(m + n))。 您可以假设nums1和nums2不能都为空。 例 1: nums1 = [1, 3] nums2 = [2] The median is 2.0 例2: nums1 = [1, 2] nums2 = [3, 4] The median is (2 + 3)/2 =...

czp_374的博客 531

VSCode + Copilot下:配置并使用 DeepSeek

以下是关于在中,通过插件配置并使用系列模型 () 的完整汇总指南。

jerwey的博客 1万+

【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

java modbus通讯协议_Modbus通 讯 协 议

电磁流量计Modbus通 讯 协 议(版本号:9000-K V9.1.1)2020年01月01日通讯协议针对KFD电磁流量计工业应用设计,版本:9000-K V9.1.1,该版本主要用于实时数据采集、流量测量、流量累计控制及部分参数的修改。一、主机系统通讯部件要求1.国际标准RS-485通讯接口部件,不小于11Bytes 的通信缓冲区(FIFO),支持1200、2400、4800、96...

weixin_39956612的博客 1019

软件测试面试题汇总

软件测试面试题汇总 测试技术面试题 ........................................................................................................................................................................ 5 1、什么是兼容

晴天的专栏 2万+

leetcode之 median of two sorted arrays

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

姚光超的专栏 9万+

奈曼-皮尔逊准则

奈曼-皮尔逊准则 如果各个假设的先验概率P(Hi)(i=0,1)P(H_i)(i=0,1)P(Hi​)(i=0,1)已知,并且每种判决的代价函数已知,这时,可以用贝叶斯准则进行判决。 如果进一步假设正确判决不付出的代价,而错误判决代价相同,即c00=c11=0,c01=c10=1c_{00}=c_{11}=0,c_{01}=c_{10}=1c00​=c11​=0,c01​=c10​=1,这时可以用最小平均错误概率准则进行判决。 在不知道先验概率P(Hi)(i=0,1)P(H_i)(i=0,1)P(Hi​)(

little_soldier的专栏 1万+

LeetCode——Median of Two Sorted Arrays

看到这个题,坑定有很多东西都忘记了吧!没事!进来就行了,你不会的差不多我也不会,我都有解释清楚,进来吧!一块成长进步! 哈哈!做完了,第一次得到那么高的评价! 一下子高兴过头了,回头来发现自己还是那么差 其实还是挺难的,我看半天都没看懂

Blackoutdragon的博客 605

IDEA出现闪退或打不开的解决方法

本身项目比较大,为了打开这个软件,可调节IDEA中安装的bin目录下有个。打开IDEA的时候过一会便闪退,可以再IDEA的右下角看到如下提示。(如果没有该提示,软件右下角也会有个红色感叹号,点开查看原因即可)闪退的多数原因大致是out of memory,内存溢出。查看任务管理器中的进程:(发现内存随时有溢出的情况)但也需要注意,该参数并不是越大越好,适中即可。只有个别原因是需要管理员权限就可执行!可以尽量减少软件的启动。

码农研究僧的博客 5948

【SECS】初识SECS协议

项目标题的第一位和第二位告诉下面有多少字节表示项目的长度。而项目的长度是指项目标题后面的字节数,这里第一个字节就是项目标题。1个字节可以表示的最大长度是0-255,所以只要项目标题后面的字节数小于等于254,都是0x01。

weixin_48618536的博客 1656

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

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

水果君の日常 6456

使用AutoDL云服务器训练YOLO计算机视觉网络模型(AutoDL+Xftp+VS Code),附详细操作步骤

AutoDL AI算力云是一个提供人工智能算力服务的云平台。丰富的GPU资源:提供多种GPU型号,包括NVIDIA的A100A800L40V100RTX 4090RTX 3090等,能满足我们在不同场景下的需求,无论是进行大规模的AI训练还是复杂的科学计算等任务,都可以找到合适的GPU资源。灵活的付费方式:支持按量付费包日包周包月等多种付费模式。对于短期的实验或不确定时长的项目,可以选择按量付费;如果有长期稳定的算力需求,预付费或包周包月则可能更划算,可以根据自己的需求和预算进行选择。集成主流框架:集成了。

Limiiiing的博客 970

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

ssm框架搭建

这个是一个在idea上成功跑起来的ssm框架项目,保存在这里方便以后使用直接下载

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

[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

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

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

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

廖雪峰Python教程[完整版]

廖雪峰的python教程整理的,已经排好版了,而且还是完整的,适合小白

【leetcode】4. Median of Two Sorted Arrays

4. Median of Two Sorted ArraysProblemSolution1、python (108ms) Problem There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two sorted arrays. The overall ru...

tianxieeryang的博客 160

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

LeetCode4. Median of Two Sorted Arrays(C++/Python)

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

qq_41562704的博客 217

LeetCode 4 — Median of Two Sorted Arrays (C++ Java Python)

题目:http://oj.leetcode.com/problems/median-of-two-sorted-arrays/ There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted arrays. The overall run time comple

龙之梦 6496

LeetCode 4 Median of Two Sorted Arrays

Problem: 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)). Solution: 使用

算法之道 1447

LeetCode 4. Median of Two Sorted Arrays

LeetCode 4. Median of Two Sorted Arrays

weixin_51709063的博客 200

Median of Two Sorted Arrays - 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 assume nums1 and num...

nullptr的博客 138

[LeetCode]2 Median of Two Sorted Arrays (C++,Python实现)

LeetCode OJ的第二题,如果有问题或者给我指点欢迎来信讨论ms08.shiroh@gmail.com 题目描述

格物致知 1128

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

0004.寻找两个正序数组的中位数(Median of Two Sorted Arrays)题目描述题目分析代码实现C++JavaPython 题目描述 给定两个大小为 m 和 n 的正序(从小到大)数组 nums1 和 nums2。 请你找出这两个正序数组的中位数,并且要求算法的时间复杂度为 O(log(m + n))。 你可以假设 nums1 和 nums2 不会同时为空。 示例 1: nums1 = [1, 3] nums2 = [2] 则中位数是 2.0 示例 2: nums1 = [1, 2]

euy的博客 395

LeetCode004 Median of Two Sorted Arrays

详细见:leetcode.com/problems/median-of-two-sorted-arrays/haracters/ 说明: 同时找两个,情况太多。精简成找一个就好了。 Java Solution: github package leetcode; /* There are two sorted arrays nums1 and nums2 of

zxwtry的博客 405
上一篇: 【leetcode】3. Longest Substring Without Repeating Characters(Python & C++)
下一篇: 【leetcode】5. Longest Palindromic Substring(Python & C++)
夏洛的网
夏洛的网 领域专家: 算法与数据结构技术领域 领域专家: 算法与数据结构技术领域
博客等级 码龄14年 1236粉丝 140原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值