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 搜索时间复杂度的时候,看到归并排序比较适合这个题目.中位数直接取即可,所以重点是排序. 再来看看治阶段,我们需要将两个已经有序的子序列合并成一个 ...
随机推荐
- 【BUUCTF】Hack World 1
[BUUCTF]Blacklist (SQL盲注) 题目来源 收录于:BUUCTF CISCN2019 华北赛区 Day2 Web1 题目描述 纯粹的SQL注入题 当输入1时,返回字符串:Hello, ...
- [AI/GPT/Chat2SQL/RAG] VannaAI
概述:VaanaAI 缘起 使用 AI 生成 SQL 的原因 数据仓库和数据湖在企业中广泛应用,但能够精通 SQL 并理解企业数据结构的人很少.AI 可以帮助商业用户使用自然语言查询数据库,生成 SQ ...
- ubuntu 22.04 deskop 无法打开terminal
系统语言设置的问题,改为汉语即可
- VulnHub2018_DeRPnStiNK靶机渗透练习
据说该靶机有四个flag 扫描 扫描附近主机arp-scan -l 扫主目录 扫端口 nmap -sS -sV -n -T4 -p- 192.168.xx.xx 结果如下 Starting Nmap ...
- 【技术分析】简单了解 AccessControl
当我们开发一个智能合约,但是里面有一些函数不能随便让别人调用,只能"拥有权限"的管理员能够调用,那么这时候我们会用到权限管理机制. 实现起来也很简单,设置一个 owner 变量,通 ...
- centos7 下全局配置最新版的golang语言开发环境
按照以下步骤进行操作: 前往Go官方网站下载页面(https://golang.org/dl/)查找最新版本的Go二进制文件. 使用wget命令下载最新版本的Go二进制文件.例如,如果最新版本是1.1 ...
- Quartz.NET - 教程 12: Quartz 的其他特性
译者注: 目录在这 Quartz.NET 3.x 教程 原文在这 Lesson 12: Miscellaneous Features of Quartz 插件 Quartz 提供了一个用于插入附加功能 ...
- leetcode每日一题:转换二维数组
题目 2610. 转换二维数组 给你一个整数数组 nums .请你创建一个满足以下条件的二维数组: 二维数组应该 只 包含数组 nums 中的元素. 二维数组中的每一行都包含 不同 的整数. 二维数组 ...
- 备份一个 VirtualizingWrapPanel ,支持虚拟化
1 using System; 2 using System.Collections.Generic; 3 using System.Diagnostics; 4 using System.Linq; ...
- MCP云托管最优解,揭秘国内最大MCP中文社区背后的运行时
作者:封崇 近期,中国第一AI开源社区魔搭(ModelScope)推出全新MCP广场,上架千余款热门的 MCP 服务.从当下火热的高德地图.网页抓取再到独家的支付宝,开发者/机构可以查看近1500种M ...