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 nums2 cannot be both empty.
分别有大小为m和n的有序数组nums1和nums2。
求两个排序数组的中值。总的运行时复杂度应该是O(log (m+n))。
您可以假设nums1和nums2不能同时为空。
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(m+n)。但题目明确要求时间复杂度需要控制在O(log(m+n)),就必须思考优化版方法啦。
小陌目前的优化方法是用寻找第k小数(因为给出的两个数组皆有序,则k对应(m+n)/2):
每次比较两个数组的第k/2个大的数:
- nums1[k/2] = nums[k/2],那么这个数就是我们要找的第K小数
- nums1[k/2] > nums[k/2],则说明第K小数不在nums2中前k/2个元素,可排除
- nums1[k/2] < nums[k/2],与2同理,排除nums1中前k/2个元素
如此操作,每次都能排除k/2的元素(递归过程中,可能会出现数组个数不到k/2的情况,并不会影响算法,从另一个数组进行补充,多取相应元素即可)。
当k=1时,找到第K小数,结束递归
- 原码附录:
- 简单粗暴法
class Solution {
public double findMedianSortedArrays(int[] nums1, int[] nums2) {
int all = nums1.length+nums2.length;
int[] result = new int[all];
for(int i=0;i<all;i++){
result[i] = (i<nums1.length)?nums1[i]:nums2[i-nums1.length];
}
Arrays.sort(result);
return (all%2) ==1 ? result[(all-1)/2]:(double)(result[all/2]+result[all/2-1])/2;
}
} - top k算法思想
class Solution {
public double findMedianSortedArrays(int[] nums1, int[] nums2) {
if((nums1.length+nums2.length)%2==0){
return (findK(nums1,0,nums1.length-1,nums2,0,nums2.length-1,(nums1.length+nums2.length)/2)
+findK(nums1,0,nums1.length-1,nums2,0,nums2.length-1,(nums1.length+nums2.length)/2+1))/2.0;
}
else{
return findK(nums1,0,nums1.length-1,nums2,0,nums2.length-1,(nums1.length+nums2.length)/2+1);
}
}
public int findK(int[] nums1,int n1,int n2,int[] nums2,int m1,int m2,int k){
int i = n2 - n1 +1;
int j = m2 - m1 +1;
if(i>j){
return findK(nums2,m1,m2,nums1,n1,n2,k);
}
if(i == 0){
return nums2[m1+k-1];
}
if(k == 1){
return Math.min(nums1[n1],nums2[m1]);
}
int mid1 = Math.min(k/2,i);
int mid2 = k - mid1;
if(nums1[n1+mid1-1]==nums2[m1+mid2-1]){
return nums1[n1+mid1-1];
}
else if(nums1[n1+mid1-1]>nums2[m1+mid2-1]){
return findK(nums1,n1,n1+mid1-1,nums2,m1+mid2,m2,k-mid2);
}
else{
return findK(nums1,n1+mid1,n2,nums2,m1,m2+mid2-1,k-mid1);
}
}
}
以下内容为可耻的自我推销,完全可忽略(。•́ωก̀。).。
此处厚脸皮的安利小透明公众号 林夏夏
夏夏大大的英雄梦,
就是能和最喜欢的你,
分享一道算法题,
温习一个编程上易忽视小细节,
品一份书香氤氲,
道最后一句晚安ヾ(◍°∇°◍)ノ゙

Median of Two Sorted Arrays---LeetCode进阶路④的更多相关文章
- Median of Two Sorted Arrays LeetCode Java
两排序好的数组,找中位数 描述There are two sorted arrays A and B of size m and n respectively. Find the median of ...
- 3.Median of Two Sorted Arrays Leetcode
难度系数:5星 /*************************************************************************** 题目:有两个排好序的数组,要求 ...
- 4. Median of Two Sorted Arrays(topK-logk)
4. Median of Two Sorted Arrays 题目 There are two sorted arrays nums1 and nums2 of size m and n respec ...
- 【算法之美】求解两个有序数组的中位数 — leetcode 4. Median of Two Sorted Arrays
一道非常经典的题目,Median of Two Sorted Arrays.(PS:leetcode 我已经做了 190 道,欢迎围观全部题解 https://github.com/hanzichi/ ...
- LeetCode 4 Median of Two Sorted Arrays (两个数组的mid值)
题目来源:https://leetcode.com/problems/median-of-two-sorted-arrays/ There are two sorted arrays nums1 an ...
- LeetCode(3) || Median of Two Sorted Arrays
LeetCode(3) || Median of Two Sorted Arrays 题记 之前做了3题,感觉难度一般,没想到突然来了这道比较难的,星期六花了一天的时间才做完,可见以前基础太差了. 题 ...
- Kotlin实现LeetCode算法题之Median of Two Sorted Arrays
题目Median of Two Sorted Arrays(难度Hard) 方案1,数组合并&排序调用Java方法 import java.util.* class Solution { fu ...
- Leetcode 4. Median of Two Sorted Arrays(二分)
4. Median of Two Sorted Arrays 题目链接:https://leetcode.com/problems/median-of-two-sorted-arrays/ Descr ...
- 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 t ...
- LeetCode 4. Median of Two Sorted Arrays & 归并排序
Median of Two Sorted Arrays 搜索时间复杂度的时候,看到归并排序比较适合这个题目.中位数直接取即可,所以重点是排序. 再来看看治阶段,我们需要将两个已经有序的子序列合并成一个 ...
随机推荐
- XYBot:一款功能强大的微信机器人,超多插件等你来玩
想象一下,拥有一个全能的微信机器人,它能帮你查天气.找新闻,甚至陪你聊天,这一切都不再是梦!XYBot,一款基于docker和pywxdll hook注入技术的微信机器人,让你的微信生活更有趣.更便捷 ...
- mybatis - [09] 动态SQL
题记部分 一.if & test 如果id,name,age不为空,则按照指定的值进行查询.如果这三者都是空(null和空字符串),则该sql执行结果为全表查询的结果集. <select ...
- 设计原则&模式:原型模式 Prototype(创建型)
定义:用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象.也就是说,这种不通过new关键字来产生一个对象,而是通过对象复制(Java中的clone或反序列化)来实现的模式,就叫做原型模式. ...
- maven - [02] settings.xml配置
maven处理配置的优先级顺序 (1)全局settings.xml(优先级★☆☆☆☆) 位于Maven安装目录的conf/settings.xml,提供系统级的默认配置,比如本地仓库位置.远程仓库列表 ...
- Vulnhub-Zico2靶机-漏扫弱口令数据库+文件包含反弹shell+zip,tar提权
一.靶机搭建 选择扫描虚拟机 选择路径即可 二.信息收集 官方信息 先看一下官网的信息 级别:中级 目标:获取root并读取标志文件 说明: Zico正试图建立自己的网站,但在选择使用什么CMS时遇到 ...
- 前端跨域方案-跨域请求代理(asp.net handler)
现在技术开发偏向于使用统一的接口处理浏览器或者app的http请求. 大家都知道因为浏览器的同源策略的原因 js直接请求webapi 接口会有一些问题,即使做好服务器端的配置 同样会有不少的 问题 ...
- python 字典使用
整理很好的文章 文章复制链接: https://mp.weixin.qq.com/s/Aj65A-uuTaARW3vvYTxvzQ 1.检查键是否存在于字典中 def key_in_dict(d, k ...
- npm淘宝镜像证书过期
前言 使用 npm 报错: npm ERR! request to https://registry.npm.taobao.org/xxx failed, reason: certificate ha ...
- dify 1.0.1无法在ollama下新增LLM模型
原来在0.15很正常,升到1.0.0之后就不行 了,再后来1.0.1出来后,以为问题都解决了,没想到还是有问题. 具体是:添加ollama是容易了,但是添加模型(比如deepsek)还是不行.表现为点 ...
- numpy -- 处理数值型数据 -- 数据分析三剑客
博客地址:https://www.cnblogs.com/zylyehuo/ NumPy(Numerical Python) 是 Python 语言中做科学计算的基础库.重在于数值计算,也是大部分Py ...