leetcode解题之4. Median of Two Sorted Arrays java (两个排序数组的中位数)

LeetCode4 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)). Java AC版本 立即下载

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

在两个排序的数组查找中位数,

使用归并,会超时。

// 时间复杂度O(N)) 空间复杂度O(1),归并超时
	public double findMedianSortedArrays(int[] nums1, int[] nums2) {
		if (nums1 == null || nums2 == null)
			return 0.0;
		int pa = 0;
		int pb = 0;
		int m = nums1.length;
		int n = nums2.length;
		int[] ans = new int[m + n];
		int idx = 0;
		while (pa < m || pb < n) {
			if (pa == m) {
				ans[idx++] = nums2[pb++];
				continue;
			}
			if (pb == n) {
				ans[idx++] = nums1[pa++];
				continue;
			}
			if (nums1[pa] < nums2[pb])
				ans[idx++] = nums1[pa++];
			else
				ans[idx++] = nums2[pb++];
		}
		if (((n + m) & 1) == 1)
			return ans[idx / 2];
		else
			// 下标从0开始
			return (ans[(n + m) / 2 - 1] + ans[(n + m) / 2]) / 2.0;

	}
参考归并排序原理及其实现

二分查找思想,将a数组的中间元素与b数组的“中间元素”相比较,从而删掉较小元素所在数组的前一半和较大元素所在数组的后一半。递归下去

	public double findMedianSortedArrays(int[] nums1, int[] nums2) {
		int N = nums1.length + nums2.length;
		if (N % 2 == 1)
			return findKth(nums1, 0, nums2, 0, N / 2 + 1);
		else
			return (findKth(nums1, 0, nums2, 0, N / 2 + 1) + findKth(nums1, 0, nums2, 0, N / 2)) / 2.0;
	}

	// 返回第k大的元素,从1开始。
	private int findKth(int[] a, int alo, int[] b, int blo, int k) {
		// a数组为空时,返回b数组第k个元素
		// k从1开始
		// alo,blo是第一个元素
		if (alo >= a.length)
			return b[blo + k - 1];
		// b数组为空时,返回a数组第k个元素
		if (blo >= b.length)
			return a[alo + k - 1];
		// 当k==1时,返回a[alo]、b[alo]中小的那个
		if (k == 1)
			return Math.min(a[alo], b[blo]);
		// i表示mid到lo之间的差值
		int i = k / 2 - 1;
		int aMid = Integer.MAX_VALUE;
		int bMid = Integer.MAX_VALUE;
		// lo+i正好为中位数下标位置
		if (alo + i < a.length)
			aMid = a[alo + i];
		if (blo + i < b.length)
			bMid = b[blo + i];
		if (aMid < bMid)
			// 在amid之前,包括amid,不可能存在中位数,跳过查找(删除),
			// alo到amid(包括)之间有i+1个数字
			// 去掉i+1个数字,原来第k大,变成第k-(i+1)大
			return findKth(a, alo + i + 1, b, blo, k - i - 1);
		else
			return findKth(a, alo, b, blo + i + 1, k - i - 1);
	}


STM32G474使用HRTIM触发多路ADC采样,通过DMA传输,通过串口打印显示,实现PWM中间时刻采样,避免开关噪声 本章工程使用HRTIM定时器进行ADC的触发,打开主定时器,子定时器A,B,C。(本工程未使用到A与C定时器,配置时可无需打开)主定时器配置:设置主定时器的频率为100KHz,并且设置自动更新以及自动重装载值。子定时器B设置:设置子定时器B为100KHz,并且每九次计数完成后产生一次中断。配置如上:1.打开自动预装载值2.打开自动复位3.设置死区时间以及互补PWM4.定时器复位触发更新寄存器5.定时器翻转重置更新6.重置触发源(定时器计数器在发生更新事件时重置) 阅读详情

相关推荐

产品经理-面试题目--持续更新

答:面试官,你好,我叫XXX,来自于XXXX,我系统的学习过软件开发,也主动的积累了不少产品方面的实战经验,在学习期间,我自己独立的完成了2个深度项目,一个是仿抖音的短视频APP全流程设计,另外一个是电商后台系统的模块,通过这些项目,我不仅熟练使用Axure等工具做出高保真原型图和清晰的产品框架,也形成了数据驱动和用户优先的思维习惯,另外,我也注重拓展自己的能力边界,拿到了CAIE人工智能证书,我希望能把AI能力融入到产品场景,提升体验和效率,我非常喜欢产品工作带来的创造性和逻辑性,希望有机会加入贵公司。

m0_63966958的博客 964

【LeetCode】No.4. Median of Two Sorted Arrays -- Java Version

【LeetCode】No.4. Median of Two Sorted Arrays -- Java Version

汤姆lazy的CSDN博客 718

ST7789驱动实战:从SPI时序到RGB565显示的完整配置解析

本文详细解析了ST7789液晶驱动芯片的SPI时序配置与RGB565显示实现,从数据手册解读到代码实战,涵盖SPI接口配置、关键寄存器设置及完整初始化流程。通过优化DMA传输和常见问题排查,帮助开发者高效驱动ST7789显示屏,适用于嵌入式显示项目开发。

weixin_29162439的博客 197

4. 寻找两个正序数组的中位数-LeetCode(Java)

寻找两个正序数组的中位数-LeetCode(Java)   ​ 寻找两个正序数组的中位数-LeetCode(Java) 题目:4. 寻找两个正序数组的中位数 给定两 分析:     1.暴力解法      (1)思路:      (2)代码:     2.进阶:归并排序      (1)思路:      (2)代码:     3.二分查找      (1)思路:      (2)代码      4.划分数组      (1)思路:      (2)代码

m0_60416689的博客 1618

万达电影 com.wandafilm.app headers check

万达电影 安卓逆向 抓包 逆向 check

weixin_43786514的博客 1444

Windows 11下ROS2 Humble与PyCharm无缝集成实战(避坑指南+完整配置流程)

本文详细介绍了在Windows 11系统下如何实现ROS2 Humble与PyCharm的无缝集成,包括环境准备、ROS2 Humble安装、PyCharm配置以及C++与Qt5混合开发的实战技巧。通过避坑指南和完整配置流程,帮助开发者高效搭建稳定的机器人开发环境,特别适合Windows平台的ROS2开发者。

weixin_28710999的博客 373

JAVA学习-练习试用Java实现“寻找两个正序数组的中位数”

这段代码的思路是:首先创建一个新的数组来合并两个输入的数组,然后将合并后的数组进行排序。解释:合并数组 = [1,2,3,4] ,中位数 (2 + 3) / 2 = 2.5。输入:nums1 = [1,2], nums2 = [3,4]输入:nums1 = [0,0], nums2 = [0,0]输入:nums1 = [1,3], nums2 = [2]输入:nums1 = [], nums2 = [1]输入:nums1 = [2], nums2 = []解释:合并数组 = [1,2,3] ,中位数 2。

weixin_69763181的博客 639

从M350 RTK到E-Port:解锁大疆无人机多负载协同与PSDK V3开发新范式

本文深入解析了大疆M350 RTK无人机搭载的E-Port智能接口与全新的PSDK V3开发套件,如何共同实现多负载协同作业的范式转变。E-Port集成了供电与高速数据总线,而PSDK V3通过统一的设备模型与服务机制,大幅降低了第三方负载的开发门槛与集成复杂度,使构建如智能巡检等复杂空中作业系统变得更加高效和标准化。

3c4x5z6v7b的博客 173

【LeetCode】4. Median of Two Sorted Arrays - Java实现

1. 题目描述: 2. 思路分析: 3. Java代码: 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 co...

小怪孩的成长之路 832

DeepSeek模型快速部署教程-搭建自己的DeepSeek

在人工智能技术飞速发展的今天,深度学习模型已成为推动各行各业智能化转型的核心驱动力。DeepSeek 作为一款领先的 AI 模型,凭借其高效的性能和灵活的部署方式,受到了广泛关注。无论是自然语言处理、图像识别,还是智能推荐系统,DeepSeek 都能提供强大的支持。本文将详细介绍 DeepSeek 的特点,并分享两种常见的部署方式:容器云部署和极速部署,帮助用户快速上手并高效利用这一先进技术。无论是技术开发者还是普通用户,都能通过这些方法快速上手并高效利用 DeepSeek 的强大能力。

嵌入式阿齐Archie 1万+

YOLOv11 改进 - SPPF模块 | 发论文神器!LSKA注意力改进SPPF,增强多尺度特征提取能力,高效涨点!!!

本文介绍了Large Separable Kernel Attention (LSKA)模块及其在YOLOv11中的结合应用。LSKA旨在解决Visual Attention Networks (VAN)使用大内核卷积时的计算效率问题,通过将2D深度卷积层的卷积核分解为级联的水平和垂直1-D卷积核,降低了计算复杂度和内存占用,且使VAN更关注物体形状。我们将集成LSKA的SPPF_LSKA模块引入YOLOv11,大量实验表明,改进后的模型在对象识别、检测、分割和稳健性测试上表现出色。

魔改工程师的博客 1287

java median of two sorted arrays,Median of Two Sorted Arrays

private int findValue(int A[], int leftA, int[] B, int leftB, int k) {if (A.length - leftA > B.length - leftB) {return findValue(B, leftB, A, leftA, k);}if (A.length - leftA == 0) {return B[k - 1];...

weixin_35809056的博客 205

使用R语言实战:构建Logistic回归模型

Logistic回归是一种基于概率的分类算法,它用于预测离散的结果变量(也称为响应变量或因变量)。与线性回归不同,Logistic回归的输出是一个概率值,通常在0和1之间。这使得它非常适合处理二分类问题。市场营销:预测客户是否会购买某个产品。医学研究:预测疾病的发生与否。信用风险评估:预测个人申请贷款的违约风险。社会科学:预测个人对某个政策的态度。

2301_79331230的博客 1143

【LeetCode】4. Median of Two Sorted Arrays 寻找两个正序数组的中位数

作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录题目描述题目大意解题方法二分查找日期 题目地址:https://leetcode.com/problems/median-of-two-sorted-arrays/ 题目描述 There are two sorted arrays nums1 and nums2 of size m...

负雪明烛 2333

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

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

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

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

老版QQ原生表情gif动态

这是老版的QQ表情,不过全是动态的的,Windows照片查看器是不会动的,可以右击用浏览器打开查看,再说一遍是老版的,只是分享给宁愿用老版也要动态的人。

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

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

水果君の日常 6456

vb 6.0并口开发操作实例

vb6并口开发,简体中文,试例内有inpout32.dll动态链接库

leetcode 4. 两个排序数组的中位数 Median of two sorted arrays

https://leetcode-cn.com/problems/median-of-two-sorted-arrays/description/ 题目描述 给定两个大小为 m 和 n 的有序数组 nums1 和 nums2 。 请找出这两个有序数组的中位数。要求算法的时间复杂度为 O(log (m+n)) 。 你可以假设 nums1 和 nums2 不同时为空。 示例 1: nums1 = [...

fxnfk 597

LeetCode 4 JAVA 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 nums...

qq_31059853的博客 519

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

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

LeetCode(4):寻找两个有序数组的中位数 Median of Two Sorted Arrays(Java)

2019.4.11 最近做了不少笔试,很多公司笔试都有在LeetCode原题上修改过来,而且最近各个公司出hard题的比例越来越高了。最近有做了腾讯、华为、百度、网易、招行、携程、心动网络、趋势科技。比如百度开发就出了三道hard,腾讯网易会有一道hard,华为最后一题根本没有思路。期待近期阿里的笔试。 这道题有两个思路: 1.两个数组归并,复杂度o(m+n) 2.快排思想,思路为从两个数组中找第...

NJU_ChopinXBP的博客 298

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

u013862444的博客 296

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

zjwreal 390

Leetcode-4 Median of Two Sorted Arrays (java)

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

yumodev 433

(Java)leetcode-4 Median of Two Sorted Arrays(寻找两个正序数组的中位数)

二分查找

不思量 492

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解题之45. Jump Game II&55. Jump Game java (跳跃游戏)
下一篇: leetcode解题之148. Sort List Java版(对链表排序)
mine_song
博客等级 码龄14年 359粉丝 252原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值