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; 再给定... 阅读详情

题目: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)).


分析:

以下思路来自脑客爱刷题。时间复杂度是O(log(min(m,n))).


class Solution {
public:
	double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
		if (nums1.empty() && nums2.empty())
			throw exception();

		if (nums1.size()>nums2.size())
			return findMedianSortedArrays(nums2, nums1);
		int lens = nums1.size(), lenl = nums2.size();
		if (lens == lenl)
		{
			double r1 = MedianEqualdArrays(nums1, 0, lens - 1, nums2, 0, lenl - 1);
			double r2 = MedianEqualdArrays_down(nums1, 0, lens - 1, nums2, 0, lenl - 1);
			return (r1 + r2) / 2;
		}
		else if ((lens + lenl) & 1)
			return find_Kth_number(nums1, nums2, (lens + lenl) / 2 + 1);
		else
		{
			int index1 = (lens + lenl) / 2, index2 = index1 + 1;
			double r1 = find_Kth_number(nums1, nums2, index1), r2 = find_Kth_number(nums1, nums2, index2);
			return (r1+r2)/2;
		}
		
	}

	double find_Kth_number(vector<int>& nums1, vector<int>& nums2, int index)
	{
		if (nums1.empty())
		{
			return nums2[index - 1];
		}
		if (nums2.empty())
		{
			return nums1[index - 1];
		}

		int lens = nums1.size(), lenl = nums2.size();
		if (index <= lens)
		{
			return MedianEqualdArrays(nums1, 0, index - 1, nums2, 0, index - 1);
		}
		else if (index>lenl)
		{
			int offset = lens + lenl - index, index1 = lens - offset, index2 = lenl - offset;
			return MedianEqualdArrays(nums1, index1 - 1, lens - 1, nums2, index2 - 1, lenl - 1);
		}
		else// (lens,lensl]
		{
			int nums2_begin = index - lens - 1, nums_end = index - 1;
			if (nums2[nums2_begin] >= nums1.back())
				return nums2[nums2_begin];
			else
			{
				return MedianEqualdArrays(nums1, 0, lens - 1, nums2, nums2_begin + 1, nums_end);
			}
		}
	}

	//返回的是比较小的那个中位数
	double MedianEqualdArrays(vector<int>& nums1, int begin1, int end1, vector<int>& nums2, int begin2, int end2)
	{
		int len = end1 - begin1 + 1;
		if (len == 1)
		{
			return min(nums1[begin1], nums2[begin2]);
		}
		else
		{
			int move_step = len / 2;
			if ((len & 1) == 0)
			{
				--move_step;
			}
			int mid1 = begin1 + move_step, mid2 = begin2 + move_step;
			if (nums1[mid1] == nums2[mid2])
				return nums1[mid1];
			else if (nums1[mid1]<nums2[mid2])
			{
				if (len & 1)
					return MedianEqualdArrays(nums1, mid1, end1, nums2, begin2, mid2);
				else
					return MedianEqualdArrays(nums1, mid1 + 1, end1, nums2, begin2, mid2);
			}
			else//nums1[mid1]>nums2[mid2]
			{
				if (len & 1)
					return MedianEqualdArrays(nums1, begin1, mid1, nums2, mid2, end2);
				else
					return MedianEqualdArrays(nums1, begin1, mid1, nums2, mid2 + 1, end2);
			}
		}
	}
	//返回的是比较大的那个中位数
	double MedianEqualdArrays_down(vector<int>& nums1, int begin1, int end1, vector<int>& nums2, int begin2, int end2)
	{
		int len = end1 - begin1 + 1;
		if (len == 1)
		{
			return max(nums1[begin1], nums2[begin2]);
		}
		else
		{
			int move_step = len / 2;
			int mid1 = begin1 + move_step, mid2 = begin2 + move_step;
			if (nums1[mid1] == nums2[mid2])
				return nums1[mid1];
			else if (nums1[mid1]<nums2[mid2])
			{
				if (len & 1)
					return MedianEqualdArrays_down(nums1, mid1, end1, nums2, begin2, mid2);
				else
					return MedianEqualdArrays_down(nums1, mid1, end1, nums2, begin2, mid2 - 1);
			}
			else//nums1[mid1]>nums2[mid2]
			{
				if (len & 1)
					return MedianEqualdArrays_down(nums1, begin1, mid1, nums2, mid2, end2);
				else
					return MedianEqualdArrays_down(nums1, begin1, mid1 - 1, nums2, mid2, end2);
			}
		}
	}
};


ZYNQ裸机双网口通信实战:手把手教你用SDK和LWIP配置PS+PL以太网(附完整源码) 本文详细介绍了在ZYNQ裸机环境下实现双网口通信的全流程,包括硬件配置、LWIP协议栈优化及TCP通信实现。通过Vivado SDK和LWIP协议栈,开发者可以高效配置PS端内置网口与PL端扩展网口,提升嵌入式网络应用的灵活性和性能。附完整源码,助力快速开发。 阅读详情

相关推荐

STM32笔记:高精度脉冲宽度计 双输入捕获+DMA方式

本文介绍如何用STM32F107VC实现高精度的脉冲宽度计(测量周期脉冲的占空比)。 采用定时器的外部输入捕获功能,双输入+DMA方式,追求硬件的极限表现。

st01lsp的专栏 5299

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

四毛毛的博客 333

vue3接入大华摄像头

┃ ┃ ┣ 📜PlaybackModal.vue 回放列表模态框。┃ ┃ ┗ 📜VideoGrid.vue 视频播放区域。┃ ┃ ┣ 📜ControlPanel.vue 控制面板。┃ ┃ ┣ 📜DeviceList.vue 设备列表。┃ ┣ 📂module //大华摄像头文件。

💻 专注 Vue 前端与 Java 后端技术 🌱 目前正在深入 Spring Boot、微服务与性能优化 📝 定期分享实战踩坑经验与学习笔记 🤝 欢迎交流,共同进步 917

【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)). 有两个排序的数组A和B分别是m和n。找到两个排序数...

码农印象 2297

codevs 1620 轮船问题 DP 解题报告

某国家被一条河划分为南北两部分,在南岸和北岸总共有N对城市,每一城市在对岸都有唯一的友好城市,任何两个城市都没有相同的友好城市。

Hawo11的博客 825

STM32F407移植CANopenNode协议栈完整实战指南

CANopen作为一种面向工业自动化与嵌入式设备的高层通信协议,广泛应用于伺服驱动、传感器网络、AGV调度等场景,其核心机制围绕对象字典、SDO、PDO与心跳报文展开。理解CANopen的通信原理,是掌握设备联网与数据交换技术的关键。在实际工程中,将CANopenNode协议栈快速移植到不同MCU平台,能显著缩短工业设备的开发周期,提升系统的可维护性与互操作性。本文以STM32F407探索者开发板为例,详细介绍CANopenNode协议栈的移植全过程,包括底层驱动对接、对象字典裁剪、定时器时基配置与Flas

weixin_29175469的博客 335

第五题,findMedianSortedArrays

Questions: There are two sorted arraysnums1andnums2of size m and n respectively. Find the median of the two sorted arrays. The overall run ti...

chezong5634的博客 643

手把手教你用yt-dlp批量下载YouTube视频数据集(附TikTok/Kaggle资源)

本文提供了一份构建视频数据集的实战指南,重点介绍了使用yt-dlp工具批量下载YouTube视频的高级技巧,包括环境配置、批量参数设置与自动化脚本。同时,文章还整合了TikTok、Kaggle及学术开源数据集等多源高质量视频资源,并分享了数据清洗、格式统一与管理的完整流程,帮助研究者和开发者高效获取与处理训练数据。

weixin_29057237的博客 310

Find the 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: n

wuh2333的博客 1546

2025年第十六届蓝桥杯省赛C/C++大学B组 个人题解

也是写了写部分题解,朱波也是狠狠水了个省二┭┮﹏┭┮,毕竟许多算法知识没怎么学。

2301_80173664的博客 2453

【AUTOSAR-E2E】-2.3-配置E2E:基于E2EPW配置一个RX接收的Signal Group(使用E2E Profile 1)

《共13页精讲》:此法也适用于E2E Profile 4/5/6/7/11/22 目录 1 检查CAN Matrix Arxml/DBC是否满足E2E需求 2 在DaVinci Developer中为Port添加E2E保护 3 在DaVinci Configurator settings设置 4 如何使用应用层的E2E保护 5 结尾 <--返回「Autosar_BSW高阶配置」总目录--> <--返回「Autosar_BSW高阶配置」专栏主页--> <-.

「汽车电子助手」的博客 3958

[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)). You may assumenums1an...

LynnZhang7的博客 318

如何完全卸载axure软件以及解决安装axure后打不开的问题【2023年】

如何完全卸载axure软件以及解决安装axure后打不开的问题【2023年】

念兮为美 5146

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

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

LC:4. Median of Two Sorted Arrays

4.Median of Two Sorted Arrays from typing import List class Solution: def findMedianSortedArrays(self, A: List[int], B: List[int]) -> float: m, n = len(A), len(B) if m &...

weixin_34221276的博客 184

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

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

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

Median of Two Sorted Arrays - LeetCode

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

weixin_30924239的博客 139

Substring with Concatenation of All Words & Median of Two Sorted Arrays & Divide Two Integers

(1) Substring with Concatenation of All Words

文森的学术之路 509

Leetcode 4 Median of Two Sorted Arrays

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

思维决定一切 3万+

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

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

贫道绝缘子的博客 259

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

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-Course Schedule
下一篇: 【广度优先遍历】leetcode - Word Ladder II
bupt8846
博客等级 码龄16年 15粉丝 86原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值