C#LeetCode刷题之#4-两个排序数组的中位数(Median of Two Sorted Arrays)
问题
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/4005 访问。
给定两个大小为 m 和 n 的有序数组 nums1 和 nums2 。
请找出这两个有序数组的中位数。要求算法的时间复杂度为 。
你可以假设 nums1 和 nums2 不同时为空。
nums1 = [1, 3]
nums2 = [2]
中位数是 2.0
nums1 = [1, 2]
nums2 = [3, 4]
中位数是 (2 + 3)/2 = 2.5
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.
nums1 = [1, 3]
nums2 = [2]
The median is 2.0
nums1 = [1, 2]
nums2 = [3, 4]
The median is (2 + 3)/2 = 2.5
中位数(又称中值,英语:Median),统计学中的专有名词,代表一个样本、种群或概率分布中的一个数值,其可将数值集合划分为相等的上下两部分。
对于有限的数集,可以通过把所有观察值高低排序后找出正中间的一个作为中位数。如果观察值有偶数个,通常取最中间的两个数值的平均数作为中位数。
这道题经过简单的思维转换其实可以转换成求第k小的值问题,首先合并2个数组,再找出第k小的值。该题需要根据原问题规模(m+n)的奇偶性做出相应调整。
示例
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/4005 访问。
public class Program {public static void Main(string[] args) {int[] nums1 = { 1, 2, 3 };int[] nums2 = { 4, 5 };var res = FindMedianSortedArrays(nums1, nums2);Console.WriteLine($"The median is {res}.");Console.ReadKey();}private static double FindMedianSortedArrays(int[] nums1, int[] nums2) {int size = nums1.Length + nums2.Length;int[] union = new int[size];int mid = size / 2;int even = (size % 2 == 0) ? 1 : 0;int m1, m2;int left, right;for(int i = m1 = m2 = 0; i < size; i++) {left = m1 < nums1.Length ? nums1[m1] : int.MaxValue;right = m2 < nums2.Length ? nums2[m2] : int.MaxValue;if(left < right) {union[m1 + m2] = nums1[m1];m1++;} else {union[m1 + m2] = nums2[m2];m2++;}if(i == mid) break;}return (union[mid] + union[mid - even]) / 2.0;}}
以上给出1种算法实现,以下是这个案例的输出结果:
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/4005 访问。
The median is 3.
分析:
显然这种解法在最坏的情况下的时间复杂度为 ,并没有达到题中要求的 ,这里仅为标记,稍后再来改进算法。
C#LeetCode刷题之#4-两个排序数组的中位数(Median of Two Sorted Arrays)相关推荐
- LeetCode刷题第二天——3Longest Substring Without repeating character 4 Median of Two Sorted Arrays...
混淆点: 子串 连续 子序列 可以不连续 知识点: HashMap: 出现问题: 1.使用unordered_map头文件时报错 #error This file requires compiler ...
- 算法—两个有序数组的中位数 Median of Two Sorted Arrays
关注微信公众号:CodingTechWork,一起学习进步. 题目 There are two sorted arrays nums1 and nums2 of size m and n respec ...
- 学渣的刷题之旅 leetcode刷题 88. 合并两个有序数组
给你两个有序整数数组 nums1 和 nums2,请你将 nums2 合并到 nums1 中,使 num1 成为一个有序数组. 说明: 初始化 nums1 和 nums2 的元素数量分别为 m 和 n ...
- LeetCode刷题(Python)——在排序数组中查找元素的第一个和最后一个位置
题目描述 给定一个按照升序排列的整数数组 nums,和一个目标值 target.找出给定目标值在数组中的开始位置和结束位置. 你的算法时间复杂度必须是 O(log n) 级别. 如果数组中不存在目标值 ...
- leetcode题库:4.两个排序数组的中位数
题目: /** *leetcode题库:4. 求两个排序数组的中位数 * * 给定两个大小为 m 和 n 的有序数组 nums1 和 nums2 . * 请找出这两个有序数组的中位数. ...
- 力扣刷题之合并两个有序数组
力扣刷题之合并两个有序数组 题目 给你两个按 非递减顺序 排列的整数数组 nums1 和 nums2,另有两个整数 m 和 n ,分别表示 nums1 和 nums2 中的元素数目. 请你 合并 nu ...
- LeetCode 4 两个排序数组的中位数
有两个大小为 m 和 n 的排序数组 nums1 和 nums2 . 请找出两个排序数组的中位数并且总的运行时间复杂度为 O(log (m+n)) . 示例 1: nums1 = [1, 3] num ...
- 两个排序数组的中位数(4.Median of Two Sorted Arrays)
题目: 有两个排序的数组nums1和nums2分别为m和n大小. 找到两个排序数组的中位数.整体运行时间复杂度应为O(log(m + n)). 示例1: nums1 = [1,3] nums2 = [ ...
- 算法-两个排序数组的中位数
题目 两个排序数组的中位数 给定两个大小为 m 和 n 的有序数组 nums1 和 nums2 . 请找出这两个有序数组的中位数.要求算法的时间复杂度为 O(log (m+n)) . 示例 1:num ...
- Leetcode4---求两个排序数组的中位数
题目:给定两个排序数组,求两个排序数组的中位数,要求时间复杂度为O(log(m+n)) 举例: Example 1: nums1 = [1, 3] nums2 = [2]The median is 2 ...
最新文章
- 原创 | 电视广告流量预测中的“常识”陷阱,你掉进去了吗?
- 长得类似铁甲小宝的机器人_铁甲小宝:小时候只顾看机器人忽略重点,长大后再看:是我太天真...
- VC++网络资源集合
- 【web安全】Spring Boot eureka xstream 反序列化
- php里h和h的区别吗,编码h264h和h264b有什么区别
- 如何在Docker中安装MySQL
- 黑马微信小程序项目实战
- [置顶] 我也来学习nodejs 没有就自己来 色色实现迷你 MVC
- donet 微服务开发 学习-熔断降级 Polly
- 制作u盘版的kail linux 系统,即插即用,用于破解邻居妹子家的wifi
- sklearn学习-SVM例程总结2(特征选择——单因素方差分析(方差分析anova ))
- android 简单的贪吃蛇小游戏
- RDP服务针对性攻击、钓鱼邮件攻击和勒索病毒家族Phobos研究
- 数据应用案例之“客户画像体系”
- 记录一下git 打patch导入patch遇到的问题
- java 1029: 三角形判定
- 云端课堂未能连接到服务器,云端课堂如何登录?详细步骤、流程介绍
- ARM9协处理器CP15及MCR和MRC指令
- 什么是零拷贝(Zero-copy)?
- 论文阅读——CcNet:A cross-connected convolutional network for segmenting retinal vessels using multi-scale
热门文章
- C++——random库中的uniform_int_distribution
- centos7.x 通过yum方式安装java 1.8.0
- Ubuntu下安装visual studio code
- 仓库对象DataSet与小车对象DataAdapter的 关键命令 1201
- composer 中国镜像
- openssh升级之后git账户免密登陆失效
- 5月5日——更改手机状态栏的背景颜色
- 互联网教育+大数据=新型大学?
- jenkins的svn路径中文问题
- UVA494 Kindergarten Counting Game