Leetcode刷题C#版之 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]
nums2 = [2] The median is 2.0
Example 2:
nums1 = [1, 2]
nums2 = [3, 4] The median is (2 + 3)/2 = 2.5
C#版本的代码如下(关键步骤已经注释):
//这道题目是一个并归排序的思想;已知有两个排好序的序列再排序
public static double FindMedianSortedArrays(int[] nums1, int[] nums2)
{
double result = ;
//定义两个游标尺分别标记两个数组的不同位置
int i = ;
int j = ;
//存放新的序列
List<int> intresult = new List<int>();
//循环直到一个序列全部排序完成
while (nums1.Length > i && nums2.Length > j)
{
//将小的数字放入新的序列中
if (nums1[i] >= nums2[j])
{
intresult.Add(nums2[j]);
j++;
}
else
{
intresult.Add(nums1[i]);
i++;
}
}
//判断哪个数组没有排完序,将之全部放入新的序列中
if (j >= nums2.Length)
{
while (nums1.Length > i)
{
intresult.Add(nums1[i]);
i++;
}
}
else if (i >= nums1.Length)
{
while (nums2.Length > j)
{
intresult.Add(nums2[j]);
j++;
} }
//计算中间值
if (intresult.Count % == )
{
result = (intresult[intresult.Count / ] + intresult[intresult.Count / - ]) / 2.0;
}
else
{
result = intresult[intresult.Count / ];
}
return result;
}
这里代码跑出来的时间为:156 ms,超过了所有提交代码的时间,小小的炫耀一下:
抛砖引玉还请大神多多指教
Leetcode刷题C#版之 Median of Two Sorted Arrays的更多相关文章
- 【LeetCode刷题Java版】Reverse Words in a String
Given an input string, reverse the string word by word. For example, Given s = "the sky is blue ...
- Leetcode刷题C#版之 Length of Last Word
题目: Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return t ...
- Leetcode刷题C#版之Toeplitz Matrix
题目: Toeplitz Matrix A matrix is Toeplitz if every diagonal from top-left to bottom-right has the sam ...
- 【leetcode刷题笔记】Find Minimum in Rotated Sorted Array
Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...
- (python)leetcode刷题笔记04 Median of Two Sorted Arrays
4. Median of Two Sorted Arrays There are two sorted arrays nums1 and nums2 of size m and n respectiv ...
- LeetCode刷题总结-数组篇(中)
本文接着上一篇文章<LeetCode刷题总结-数组篇(上)>,继续讲第二个常考问题:矩阵问题. 矩阵也可以称为二维数组.在LeetCode相关习题中,作者总结发现主要考点有:矩阵元素的遍历 ...
- C#LeetCode刷题-分治算法
分治算法篇 # 题名 刷题 通过率 难度 4 两个排序数组的中位数 C#LeetCode刷题之#4-两个排序数组的中位数(Median of Two Sorted Arrays)-该题未达最优解 30 ...
- C#LeetCode刷题-二分查找
二分查找篇 # 题名 刷题 通过率 难度 4 两个排序数组的中位数 C#LeetCode刷题之#4-两个排序数组的中位数(Median of Two Sorted Arrays)-该题未达最优解 30 ...
- C#LeetCode刷题-数组
数组篇 # 题名 刷题 通过率 难度 1 两数之和 C#LeetCode刷题之#1-两数之和(Two Sum) 43.1% 简单 4 两个排序数组的中位数 C#LeetCode刷题之#4-两个排序数组 ...
随机推荐
- debian 9 双显卡安装NVIDIA显卡驱动
最近用debian,给debian装n卡驱动折腾了好几天了,主要还是网络不好,官方wiki的方法下载经常卡死..摸索了几天感觉已经摸到了头绪,决定写下来供大家参考参考 先提供单显卡NVIDIA驱动的安 ...
- 什么是A记录/CNAME记录/MX记录/TXT记录
答: A 记录(Address)是用来指定主机名(或域名)对应的IP地址记录.当你输入域名的时候给你引导向设置在DNS的A记录所对应的服务器. CNAME记录 ( Canonical Name )是一 ...
- 【WebApi系列】浅谈HTTP
[01]浅谈HTTP在WebApi开发中的运用 [02]聊聊WebApi体系结构 [03]详解WebApi如何传递参数 [04]详解WebApi测试和PostMan [05]浅谈WebApi Core ...
- 认识Linux分区
前言 今年目标是熟练Linux系统与内核,没有老司机带只能自己慢慢参照鸟哥教程学习了.如果有老司机麻烦指导一下便捷路线,作为这方便的新手还是很乐意接受各位的意见.今天第一步就是熟悉安装Linux中分区 ...
- Android-第一天
1.google 2.application->application framework->libraries(调用关系) 3.strings.xml 是全局字符串的配置文件 4.ADT ...
- pip install在Windows下报错解决
报错: Traceback (most recent call last): File, in<module> load_entry_point('pip==1.4.1','console ...
- 阿里云Maven配置,Maven仓库配置,Maven镜像配置
阿里云Maven配置,Maven仓库配置,Maven镜像配置 ======================== 蕃薯耀 2018年1月29日 http://www.cnblogs.com/fanshu ...
- mybatis_SQL映射(4)鉴别器
摘录自:http://blog.csdn.net/y172158950/article/details/17505739 鉴别器:有时一个单独的数据库查询也许返回很多不同(但是希望有些关联)数据类型的 ...
- Vuejs 安装与配置
1.全局安装 vue-cli $ npm install --global vue-cli 2.创建一个基于 webpack 模板的新项目 $ vue init webpack my-project ...
- tomcat三种启动不同的启动方式
Linux下tomcat服务的启动.关闭与错误跟踪,通常通过以下几种方式启动关闭tomcat服务: 切换到tomcat主目录下的bin目录 1. 启动tomcat服务 方式一:直接启动 ./start ...