问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 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)的更多相关文章

  1. [Swift]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 ...

  2. leetcode刷题四<寻找两个有序数组的中位数>

    给定两个大小为 m 和 n 的有序数组 nums1 和 nums2. 请你找出这两个有序数组的中位数,并且要求算法的时间复杂度为 O(log(m + n)). 你可以假设 nums1 和 nums2 ...

  3. 《LeetCode-0004》 寻找两个有序数组的中位数-Median of Two Sorted Arrays

    题目给定两个大小为 m 和 n 的有序数组nums1和 nums2. 请你找出这两个有序数组的中位数,并且要求算法的时间复杂度为 O(log(m + n)). 你可以假设 nums1 和 nums2 ...

  4. 力扣 -- 寻找两个有序数组的中位数 Median of Two Sorted Arrays python实现

    题目描述: 中文: 给定两个大小为 m 和 n 的有序数组 nums1 和 nums2. 请你找出这两个有序数组的中位数,并且要求算法的时间复杂度为 O(log(m + n)). 你可以假设 nums ...

  5. 两排序数组的中位数 Median of Two Sorted Arrays

    2018-11-18 23:33:28 问题描述: 问题求解: 这个问题是一个比较有难度的可以使用二分搜索法求解的问题,如果采用朴素的解法进行merge再找中位数的话,其时间复杂度为O(n1 + n2 ...

  6. LeetCode(4):两个排序数组的中位数

    Hard! 题目描述: 有两个大小为 m 和 n 的排序数组 nums1 和 nums2 . 请找出两个排序数组的中位数并且总的运行时间复杂度为 O(log (m+n)) . 示例 1: nums1 ...

  7. #leetcode刷题之路26-删除排序数组中的重复项

    给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度.不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成. 示例 1: ...

  8. leetcode刷题-88.合并两个有序数组

    题目 给你两个有序整数数组 nums1 和 nums2,请你将 nums2 合并到 nums1 中,使 nums1 成为一个有序数组. 说明: 初始化 nums1 和 nums2 的元素数量分别为 m ...

  9. Leetcode(4)寻找两个有序数组的中位数

    Leetcode(4)寻找两个有序数组的中位数 [题目表述]: 给定两个大小为 m 和 n 的有序数组 nums1 和* nums2. 请你找出这两个有序数组的中位数,并且要求算法的时间复杂度为 O( ...

  10. LeetCode4. 两个排序数组的中位数

    4. 两个排序数组的中位数 问题描述 There are two sorted arrays nums1 and nums2 of size m and n respectively.Find the ...

随机推荐

  1. 实践使用nodejs获取用户真实IP?

    先上代码 var http = require('http') var server = http.createServer(function (req,res) { console.log(req. ...

  2. 使用数据泵(expdp、impdp)迁移数据库流程

    转载原文地址为:http://blog.itpub.net/26736162/viewspace-2652256/ 使用数据泵迁移数据库流程 How To Move Or Copy A Databas ...

  3. 【C#】根据开始时间和结束时间筛选存在的信息

    背景 业务需求中,需要根绝开始时间和结束时间筛选一段时间内的任务存在个数. 示例图片 根据开始时间 9:00到 结束时间11:00 筛选信息 总共有这么四种情况可能出现 插入测试数据 CREATE T ...

  4. Python新手学习raise用法

    当程序出现错误时,系统会自动引发异常.除此之外,Python也允许程序自行引发异常,自行引发异常使用 raise 语句来完成. 很多时候,系统是否要引发异常,可能需要根据应用的业务需求来决定,如果程序 ...

  5. 使用SQL语句建表,插入数据

    --选中数据库,点击新建查询,然后执行即可--这是SQL中的注释信息,使用两个减号来注释. drop table Book --删除表Book create table Book --创建表Book ...

  6. spring学习(四)使用注解代替xml配置

    用的是IDEA的maven工程,pom.xml文件导包依赖省略 一.书写要导入容器的实体类 import org.springframework.beans.factory.annotation.Va ...

  7. VS Code小白使用教程

    本文来自作者:你不知道的巨蟹 原文链接 https://www.cnblogs.com/tu-0718/p/10935910.html,如有侵权,则可删除. 前言 现在使用Vscode编码的人越来越多 ...

  8. MySQL中change与modify的用法与区别

    浅析MySQL中change与modify的区别   MySQL版本 show variables like 'version'; 表结构 desc student; 修改表 例如:修改表studen ...

  9. LRU cache缓存简单实现

    LRU cache LRU(最近最少使用)是一种常用的缓存淘汰机制.当缓存大小容量到达最大分配容量的时候,就会将缓存中最近访问最少的对象删除掉,以腾出空间给新来的数据. 实现 (1)单线程简单版本 ( ...

  10. Python globals和locals函数_reload函数

    Python globals和locals函数_reload函数: globals( ): 返回所有能够访问到的全局名字 num = 5 sum = 0 def add(num): func_sum ...