Leetcode Median of Two Sorted Arrays (java)

解法一:

import java.util.Arrays;
class Solution {
    public double findMedianSortedArrays(int[] nums1, int[] nums2) {
        //存储合并后的数组
        int[] nums3 = new int[nums1.length + nums2.length];
        for (int i = 0; i < nums3.length; i++) {
             if(i < nums1.length){
                 nums3[i] = nums1[i];
             }
             else {
                 nums3[i] = nums2[i-nums1.length];
             }
        }
        //进行升序排序
        Arrays.sort(nums3);
        //寻找median
        double median;
        if(nums3.length % 2 == 1){
             median = nums3[(nums3.length/2) ];
        }
        else{
             median = (nums3[((nums3.length/2))]+nums3[(nums3.length/2)-1])/2.0;
        }
        return median;
    }
}

该解法思路是把两个数组合并,进行升序排序,取中间值

 

解法二:

class Solution {
    public double findMedianSortedArrays(int[] nums1, int[] nums2) {
       int n = nums1.length;
       int m = nums2.length;
       //让nums1始终最短
       //若num2比nums1长会出现数组越界问题
       if(n > m){
           return findMedianSortedArrays(nums2,nums1);
       }
       int l1;//nums1左部分最右的元素
       int r1;//nums1右部分最左的元素
       int c1;//nums1分割位置
       int l2;//nums2左部分最右的元素
       int r2;//nums2右部分最左的元素
       int c2;//nums2分割部分
        //让数组恒为奇数
       int start = 0;
       int end = 2 * n + 1 - 1;
       //开始不断分割nums1
       while (start <= end){
           //c1 c2保证了是前m+n个数 数组总长为2(m+n)
           c1 = (start + end) / 2;
           c2 = m + n - c1;

           l1 = c1 == 0 ? Integer.MIN_VALUE : nums1[(c1 - 1 ) / 2];
           r1 = c1 >= 2*n ? Integer.MAX_VALUE : nums1[(c1) / 2];

           l2 = c2 == 0 ? Integer.MIN_VALUE : nums2[(c2 - 1) / 2];
           r2 = c2 >= 2*m ? Integer.MAX_VALUE : nums2[(c2) / 2];

           //l1减少 r2增大
           if(l1 > r2){
               end = c1 - 1;
           }
           //r1增大 l2减少
           else if(l2 > r1){
               start = c1 + 1;
           }
           else {
               double median = ( Math.max(l1,l2) + Math.min(r1,r2) ) / 2.0;
               return median;
           }
       }
       return 0.0;
    }
}

 该解法思路是:

  用两个分割位置把两个数组各自分成两部分,两个数组即是四部分,为l1,r1,l2,r2

  由于取的是中值,即是取第m+n个(这个做法是因为把数组长度都假设成了2*n + 1个来让数组为奇数个)

  分割的位置先是第一个数组的一半,第二个数组是 m+n - 第一个数组的位置,为了保证两个数组的左部分加起来个数是m+n来取到第m+n个数

  并且要保证两个左半部分要恒小于两个右半部分,即是l1<r1&&l1<r2 和 l2<r2&&l2<r1 因为已经是有序数组,所以有一半可以忽略,即是 l1<r2 && l2<r1

  若不满足上述条件,就把分割位置进行移动,此时正确的位置肯定在其中一边,把另一边舍去后继续二分直到符合上述条件

  当满足上述条件,l1和l2最大的,r1和r2最小的,它们的和除以2就可以得出答案

关于边界问题:

  只要保证nums1<=nums2即可,因为若出现nums1>nums2的时候,会出现nums2的分割位置出现数组越界的情况

 

github地址:https://github.com/CyanChan/leetcode

  

转载于:https://www.cnblogs.com/CyanChan/p/8509196.html

从外包到转正:一个华为OD亲历者的真实工作体验与避坑指南 本文分享了一位华为OD员工从外包到转正的真实经历与实用建议。通过详细解析华为OD的日常工作、转正核心指标、机试备考技巧及职场突围策略,为读者提供了一份全面的避坑指南。特别针对机试E卷的高频考点和刷题方法,给出了具体的数据分析和实战经验。 阅读详情

相关推荐

python 携程登陆_Python 学习练习之用户登录接口

目的:用python实现简单用户注册,登陆,加黑功能,放个流程图吧。以下内容仅为练习 执行流程:[root@zabbix login]# python user_login.pyusername:tompassword:123456Are you want to register ? [y/n]yRegister successful,Please relogin ![root@zabbix lo...

weixin_28679635的博客 525

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

浅析ArcGis中的软件——ArcMap、ArcScene、 ArcGlobe、ArcCatalog

ArcMap:是集空间数据显示、矢量数据编辑、冲突数据检查(拓扑检查)、查询检索(地理编码)、统计(字段属性表)、报表生成、空间分析(ArcGIS Spatial Analyst)和高级制图(制图表达)等众多功能干一体的桌面应用平台软件。而且提供所见即所得的符号编辑器,令用户可以随心所欲地生成任意复杂的点、线、面符号。所支持的空间数据格式包括Arclnfo coverages、ESRI shapefiles、ArcSDE lavers、DXF、DWG、DGN、常见的图像格式、GRIDs、TINs等。

lxlmycsdnfree的博客 6583

java median of two sorted arrays,[leetcode_4]Median of Two Sorted Arrays

说实话,这个题我第一次没解出来,是看了别人的思路才搞定的,中间在边界问题上还错了n次。惭愧。先说一下解题思路。一开始我能想到的思路是:merge sort然后输出array[median]即可。但是很明显这样的时间复杂度高于o(log(m+n)),大概是o(m+n)的复杂度。时间复杂度的限制是解这个题比较让人郁闷的地方。ok,再看题目条件,两个已经排好序的数组,时间复杂度是log,这样很容易让人想...

weixin_34338129的博客 309

MT6771 Android13 背光曲线调试与光感数据校准实战

本文详细介绍了MT6771平台在Android 13系统上进行背光曲线调试与光感数据校准的实战经验。针对高亮度环境下背光误调节问题,深入分析了光感数据上报机制,并提供了驱动层配置修改、背光曲线优化及常见问题解决方案,帮助开发者快速定位并解决类似问题。

moon9的博客 294

C++设计模式之观察者模式

观察者模式(Observer Pattern)是一种行为设计模式,它定义了对象之间的一对多依赖关系,当一个对象状态发生变化时,所有依赖于它的对象都会得到通知并自动更新。这种模式通常用于实现分布式事件处理系统,当一个对象(称为“主题”或“发布者”)改变状态时,所有注册的观察者都会收到通知。

每天进步一点点。 607

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

企业如何开展BI建设--指标体系建设

很多企业的数字化转型工作都是从做BI项目开始的,很多企业认为,BI就是做指标大屏,所以做了很多的驾驶舱,也做了很多指标,有从系统取的,有手工填报的,甚至手工填报的比例占到了70%,最终BI沦为了填报系统,业务认为BI就是把从线下的微信沟通转到了线上录入展示,并没有给业务代来任何提升,久而久之,BI大屏就没人关心,公司高层也认为数据都是填报的,都经过了加工处理,这种数据不可信,不能反映业务开展的实际。其实在BI系统建设之前,我们首先要做的就是要梳理企业的指标,到底我们要展示哪些指标?

267

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

AgentScope实战训练营-Java开发HITL人机协作AI应用完整指南

本文手把手带你从零搭建一个基于 Spring Boot WebFlux + AgentScope + 通义千问的人机协作(HITL)AI 聊天应用。项目涵盖流式对话、工具调用、高危操作确认、对话中断、动态 MCP 工具接入、中英文双语界面等完整功能。文章从环境准备、pom.xml 配置、DTO 数据模型、Hook 机制、AgentService 核心服务、ChatController 接口到前端 HTML/CSS/JS 实现,逐一讲解每行代码的设计意图,并附带 REST API 接口说明、测试场景、常见问题

program哲学 724

基于STM32LXXX的模数转换芯片ADC(TLA2528IRTER)驱动C程序设计

define TLA2528_ADDR 0x12 << 1 // 用于HAL传输的8位地址(写入为0x24,读取为0x25)// 100kHz (示例值,需根据实际时钟计算)#define TLA2528_REG_PIN_CFG 0x00 // 引脚配置寄存器。#define TLA2528_REG_CHN_SEL 0x01 // 通道选择寄存器。

myron6688的博客 239

LeetCode解法java:4. Median of Two Sorted Arrays

思路:这个题需要求中位数,有两个思路:1.直接将两个数组放到一个数组里排序2.因为两个数组是有序数组,所以用归并排序的思路对数组进行处理,也可以达到给数组排序的效果,但是时间复杂度能达到O(log (m+n))思路1:的代码,直接调用java本身给出的api,将两个数组放到一个长度为两个数组之和的数组里,然后将数组排序,取中间的一个或两个数的平均数,返回就可以了,时间复杂度O(nlogn)。pub...

wangdatiancai的博客 243

【即插即用模块】卷积篇 | TCSVT 2025 | MFACB:多尺度膨胀融合,快速扩大感受野,多尺度特征精准捕捉!

本文提出了一种新型多尺度融合膨胀卷积模块MFACB,用于增强语义分割任务中的特征表达能力。该模块通过三分支串联膨胀卷积结构快速扩大感受野,采用双路径融合机制(基础信息路径+多尺度信息路径)实现高效上下文聚合。相比传统卷积块,MFACB在保持轻量化的同时显著提升了多尺度特征捕捉能力,实验表明其性能优于基础残差块。该模块支持自定义膨胀率和步长调整,可灵活嵌入不同网络阶段,适用于需要特征增强的视觉任务。

主要更新底层视觉(去噪、超分等)相关的科研内容,形式为【论文精读】+【论文复现】 317

java 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 O(log (m+n)).Example 1:nums1 = [1, 3]num...

weixin_42549496的博客 302

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

[LeetCode][4]Median of Two Sorted Arrays解析 -Java实现

Q: 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)). A: 这个题目大致是说给你两个被排序

一些奇奇怪怪的解法 684

C# WinForm 基于DirectShow.dll编写的程序,可边预览边保存视频,音频和图片

基于微软的DirectShow.dll的二次开发1.自动识别连接的摄像头跟麦克风且可以对其进行选择(默认选中第一个)2.可录制wmv和avi两种格式的视频并且都带有音频,avi可以录制的分辨率和帧数进行选择,wmv默认进行了淸晰度优化如不喜欢可去除代码中有相关提示3.可在预览点开之后进行拍照,也可边录制视频边拍照4.添加了视频和音频的压缩功能(酌情使用)这次二次开发发现C#对DirectShow的相关信息太少,这次做完也不敢藏私共享出来,还望大家指正

LeetCode 第四题 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...

weixin_44147866的博客 263

Outlook Express 6 (OE6) for Win7/Win8/Win10

英文版 Outlook Express6.0, 兼容Win7/Win10/WinXP,解压就可用。

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 (java)

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

懒人部落 653

Java [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 O(log (m+n)). 解题思路: 看到时间复杂度的...

dkk37351的博客 58

LeetCode : Median of Two Sorted Arrays [java]

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)). 思路:类似于归并排序,找到一半的排好序的序列,最

蓝色街灯 的博客 249

【leetcode with java】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)). Tags: Divide and Conquer, A

满脸猪头肉 485

Leetcode-Median of Two Sorted Arrays (java)

题目 给定两个排序过的数组,长度为m、n,找到两个数组的中位数,时间复杂度要求O(log(m+n))。 解答 该题的主要难点在于对时间复杂度的要求,若是没有要求,较为直观的解法是先将两个排序数组合并为一个数组,再计算中位数,显然时间复杂度为O(n+m),不能满足题目要求。观察该题的时间复杂度,有一个log存在,一般需要对半处理的情况时间复杂度里含有log,例如二分法。但是该题

zdm472033838的博客 239

java median of two sorted arrays,[Leetcode] Median of Two Sorted Arrays 有序数组中位数

Median of Two Sorted ArraysThere 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)).有两...

weixin_33526515的博客 146

leetcode 004 Median of Two Sorted Arrays(java)

Median of Two Sorted Arrays My Submissions Question Total Accepted:75699Total Submissions:428881Difficulty:Hard There are two sorted arraysnums1andnums2of size m and n r...

diaojiqiu2713的博客 139

4. Median of Two Sorted Arrays【leetcode】java,算法,中间值

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

diaocongdie2176的博客 124

leetcode Median of Two Sorted Arrays(Java)

题目链接:点击打开链接 类型:合并数组 解法:遍历数组,排列生成新的数组 public class Solution { public double findMedianSortedArrays(int[] nums1, int[] nums2) { int len1 = nums1.length; int len2 = nums2.length;

qq_19442771的博客 193
上一篇: Leetcode Longest Substring Without Repeating Characters (java)
WANG1982106
博客等级 码龄11年 0粉丝 0原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值