leetcode Median of Two Sorted Arrays

图解LeetCode4:Median of Two Sorted Arrays 文章目录理解题意思路1:最直观解法思路2:二分查找代码实现结论 Leetcode算法系列将详细讲解一些经典的面试算法题。 今天的算法是LeetCode中第四个题目,Median of Two Sorted Arrays,也就是给定两个有序数组求出中位数。   理解题意 首先给定的是两个有序数组,比如{1,3}以及{2},那么合并这两个数组就是{1,2,3},因此很显然中位数是2; 再给定... 阅读详情

题目:

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)).
题目来源:https://leetcode.com/problems/median-of-two-sorted-arrays/

分析:

在两个排好序的数组里面找中位数。中位数的定义是,如果一共奇数个数,则排序后中间的数便是中位数;如果一共偶数个数,则排序后中间的两个数便是中位数。既然两个数组已经排好序,则可以利用merge的思路对两个数组进行排序。当然也可以不存放排序的结果,只记录下排到第几个了就行,当cnt计数器计到中位数的位置了,就得出结果了。

代码:

class Solution {
public:
    double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
        int n1 = nums1.size();
        int n2 = nums2.size();
        int med = (n1 + n2)/2;
        if((n1+n2)%2 == 1)
            med += 1;
        int p1 = 0, p2 = 0, tmp = 0, cnt = 0;
        while(p1 < n1 && p2 < n2){
            if(nums1[p1] < nums2[p2]){
                tmp = nums1[p1++];
            }
            else{
                tmp = nums2[p2++];
            }
            cnt++;
            if(cnt == med){
                if((n1+n2)%2 == 1)
                    return (double)tmp;
                else{
                    double next;
                    if(p1 >= n1)//针对偶数个数字,中间的两个数一个在结尾另一个在开头。
                        next = nums2[p2];
                    else if(p2 >= n2)
                        next = nums1[p1];
                    else
                        next = nums1[p1] < nums2[p2] ? nums1[p1] : nums2[p2];
                    return double(next + tmp) / 2;
                }
            }
        }
        while(p1 < n1){
            tmp = nums1[p1++];
            cnt++;
            if(cnt == med){
                if((n1+n2)%2 == 1)
                    return tmp;
                else
                    return (double)(tmp + nums1[p1]) / 2;
            }
        }
        while(p2 < n2){
            tmp = nums2[p2++];
            cnt++;
            if(cnt == med){
                if((n1+n2)%2 == 1)
                    return tmp;
                else
                    return (double)(tmp + nums2[p2]) / 2;
            }
        }
        return 0;
    }
};
【力扣Leetcode题解系列之0004. Median of Two Sorted Arrays:寻找两个正序数组的中位数解题攻略】 本文介绍了力扣算法题"Median of Two Sorted Arrays"的解题方法。给定两个正序数组,要求在O(log(m+n))时间复杂度内找到它们的中位数。核心思路是使用二分查找,通过合理划分两个数组的位置,使得左半部分元素都小于右半部分。文章提供了C++和Go两种语言的实现代码,并分析算法的时间复杂度为O(log(m+n)),空间复杂度为O(1)。该解法充分利用二分查找的高效性,是解决这类问题的经典方法。 阅读详情

相关推荐

【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)).问题分析首先

梦想起飞的地方 2001

LeetCode刷题笔记(链表):median-of-two-sorted-arrays

转载请注明作者和出处:http://blog.csdn.net/u011475210 代码地址:https://github.com/WordZzzz/Note/tree/master/LeetCode 刷题平台:https://www.nowcoder.com/ta/leetcode 题  库:Leetcode经典编程题 编  者:WordZzzz 题目描述 解题思路 C版代码实现

WordZzzz 1570

Amesim基础篇-元件详解-流体库管路

Amesim基础篇-元件详解-流体库管路

2201_75375391的博客 4537

Leetcode 4 median-of-two-sorted-arrays题解

Leetcode median-of-two-sorted-arrays题解 虽然很简单,但是值得纪念一下,第一次超过100%的人 题目链接 思路: **Talking is no use, show your my codes** 代码: class Solution { public double findMedianSortedArrays(int[] nums1, int[] n...

qq_42308861的博客 319

akshare 布林通道策略

import datetime import pandas as pd import backtrader as bt import matplotlib.pyplot as plt from datetime import datetime import matplotlib import akshare as ak %matplotlib inline class Boll_strategy(bt.Strategy): #自定义参数,每次买入1800手 params=(('size'.

qq_26742269的博客 5355

ANSYS18.2 workbench入门

ANSYS18.2 入门教程一:页面简单介绍 1.ANSYS解题分析流程 1.1.分析问题步骤 这一过程要求大家有一定的力学知识和和分析问题的能力。 1.2.软件分析步骤 2.workbench用户页面介绍 因为workbench其可以理解为一个软件集合包,其里面有诸多模块,其操作页面也有所不同,现分页面·为大家简要介绍。 2.1.主级菜单页面介绍 1.File菜单:主要提供保存,另存为,导...

secretboys的博客 1万+

LeetCode 4. 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(log (m...

孑辞的博客 413

从CLIP到AnomalyCLIP:零样本异常检测的跨领域进化之路

本文探讨了从CLIP到AnomalyCLIP的技术演进,重点介绍了零样本异常检测在跨领域应用中的突破。AnomalyCLIP通过对象无关提示学习和多尺度特征提取,显著提升了工业质检、医疗影像等领域的检测精度,实现了高达96.4%的金属螺母划痕检测精度和85.7%的皮肤癌敏感度。这一技术为多模态模型在异常检测中的应用开辟了新路径。

r2s3t4的博客 117

Median of Two Sorted Arrays - LeetCode

目录 题目链接 注意点 解法 小结 题目链接 Median of Two Sorted Arrays - LeetCode 注意点 要考虑数组长度不一致的情况 解法 解法一:将两个数组合并成一个有序数组,根据数组长度分奇偶情况求中位数。时间复杂度为O(m+n) c...

weixin_30924239的博客 139

数字电路复位信号设计(异步复位、同步释放)笔记详解

复位信号设计(异步复位、同步释放)笔记详解 1 时序设计 1.1 复位信号设计(异步复位、同步释放) 1.1.0 复位恢复时间和清除时间 首先解释一下恢复时间(recovery time)和清除时间(removal time):(以复位信号低电平有效为例) 复位恢复时间(recovery time):撤销复位时,复位信号无效到时钟有效沿来临之间需要维持的最小的时间,类似于同步时钟的建立时间setup time; 复位清除时间(removal time):复位时,在时钟有效沿来临之后复位信号还需要保持复位

weixin_42294124的博客 1万+

UE5Could not be compiled. Try rebuilding from source manually.

UE5Could not be compiled. Try rebuilding from source manually.

weixin_45417260的博客 2777

LeetCode每天刷day4:Median of Two Sorted Arrays

题目链接:Median of Two Sorted Arrays C++: class Solution { public: double findMedianSortedArrays(vector&lt;int&gt;&amp; nums1, vector&lt;int&gt;&amp; nums2) { int len1 = nums1.size(); ...

Jos's blogs 168

uniapp(安卓 | 苹果) - 最新详细实现App打包发布教程,uni-app苹果IOS软件应用上架App Store软件商店,uni-app打包发布安卓Android软件到各大应用商店(详细教程

uni-app,安卓android,苹果ios,uniapp App软件,Uniapp上架发布应用,打包构建,apk、app软件,应用商店,应用市场,app store,uni-app安卓苹果app上架应用商店,app应用从创建到上架_uniapp打包app的流程和上架,uniapp打包上架ios Appstore 最全流程,苹果应用商店上架教程,uniapp生成的app怎么上架到iphone的app store,uni-app打包到上架Android , ios完整详细流程,审核一直不通过,软件商店审核失

✅ 精选推荐文章 1万+

LeetCode-4. Median of Two Sorted Arrays [C++][Java]

Given two sorted arraysnums1andnums2of sizemandnrespectively, returnthe medianof the two sorted arrays. The overall run time complexity should beO(log (m+n)).

贫道绝缘子的博客 258

【SI案例分享】1000 BASE-T1共模终端的分析与评估

以太网的MDI接口应用中,共模端接网络的应用广泛,并且,其电路参数从100 BASE-T1到1000 BASE-T1保持了相同的参数定义,这就不得不引起使用者的怀疑,毕竟百兆和千兆以太网的工作频段差异巨大,这样的设计真的可以做到上下兼容吗?本文针对此问题,进行了深入的探讨。

一只豌豆象的博客 1377

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

LeetCode 4. Median of Two Sorted Arrays 题解

本题是二分查找的经典问题,主要考察对二分查找算法的理解和应用。通过将问题转化为在两个有序数组中找到第 k 小的元素,我们可以使用二分查找来高效地解决这个问题。二分查找的核心思想是:使用两个指针分别指向查找区间的两端,计算中间位置,比较中间元素与目标值的大小,根据比较结果缩小查找区间,直到找到目标值或确定目标值不存在。这种方法的时间复杂度为 O(log(min(m, n))),适用于大规模有序数组的中位数查找。掌握二分查找的原理,对于理解搜索算法和算法设计思想非常重要。

cannonjinx的博客 1010

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

leetcode之 median of two sorted arrays

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

姚光超的专栏 9万+

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

廖雪峰Python教程[完整版]

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

Leetcode 4 Median of Two Sorted Arrays

今天发现了leetcode上面一道题,觉得非常经典,记录之。 题目是这样的:给定两个已经排序好的数组(可能为空),找到两者所有元素中第k大的元素。另外一种更加具体的形式是,找到所有元素的中位数。本篇文章我们只讨论更加一般性的问题:如何找到两个数组中第k大的元素?不过,测试是用的两个数组的中位数的题目,Leetcode第4题 Median of Two Sorted Arrays 方案1:假

思维决定一切 3万+

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平台上的Median of Two Sorted Arrays题目用Java快排实现

Leetcode平台上的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 overal

sophie2805的专栏 766

LeetCode 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 complexity should be O(log (m+n)).思路分析:这题容易想到O(m+n)的解法,就是先Merge两个数组,然后返

Coding for Dreams 1734

【leetcode长跑】开个头 Median of Two Sorted Arrays

大家好,我叫张小猪。 自第一篇收集向的文章发布后,近 1 年半没更新这个专栏了。最近面试中发现将近 60% 的候选人对于 bubble sort 面露难色,于是心悸于自己也忘记了很多大学的内容,遂有时间就写写 leetcode 好了,也不荒废当初开了这个地方。路途遥远,且行且珍惜。 今天是 Algorithms 分类中第一个 Hard 的...

weixin_33790053的博客 168

LeetCode——Median of Two Sorted Arrays

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

Blackoutdragon的博客 605

leetcode-Median of Two Sorted Arrays

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

icyyyer的博客 276

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)). You may assume num...

lishinho的博客 301

leetcode MEDIAN OF TWO SORTED ARRAYS

MEDIAN OF TWO SORTED ARRAYS 原文地址:http://oj.leetcode.com/problems/median-of-two-sorted-arrays/ 题意:寻找两个有序数组的中位数。如果m+n是奇数则返回中间的值,否则返回中间两个数的平均值。 解题思路:寻找第K大的数。 算法很快就想到了,我想到的算法稍微有点儿麻烦,需要考虑的情况比较多,主要思想就

lxxstar1226的专栏 755
上一篇: 用户级线程和内核级线程
下一篇: leetcode Maximum Gap
simon-zhao
博客等级 码龄14年 58粉丝 100原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值