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-两个排序数组 ...
随机推荐
- Vue版本过渡变化
到了2.0以后,有哪些变化: 在每个组件模板,不在支持片段代码 之前: <template id="aaa"> <h3>我是组件</h3>< ...
- dedecms织梦自定义表单发送到邮箱-用163邮箱发送邮件
https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=1&tn=monline_3_dg&wd=dedecms 邮箱&oq=d ...
- linux利用sendmail发送邮件的方法
Linux利用sendmail发送邮件, 方法1 安装sendmail即可使用, mail -s "test" user@sohu.com bin/mail会默认使用本地sendm ...
- Code4 APP
MJExtension 斯坦福大学公开课
- 【开发技术】常用正则表达式大全!(例如:匹配中文、匹配html)
匹配中文字符的正则表达式: [u4e00-u9fa5] 评注:匹配中文还真是个头疼的事,有了这个表达式就好办了 匹配双字节字符(包括汉字在内):[^x00-xff] 评注:可以用来计算字符串的长度 ...
- Django权限机制的实现
Django权限机制的实现 1. Django权限机制概述 权限机制能够约束用户行为,控制页面的显示内容,也能使API更加安全和灵活:用好权限机制,能让系统更加强大和健壮.因此,基于Django的开发 ...
- 【问题处理】mysql sleep 连接数过多
睡眠连接过多,会对mysql服务器造成什么影响?严重消耗mysql服务器资源(主要是cpu, 内存),并可能导致mysql崩溃.造成睡眠连接过多的原因?1. 使用了太多持久连接(个人觉得,在高并发系统 ...
- 解决Flink输出日志中时间比当前时间晚8个小时的问题
Flink安装在CentOS7上,默认时间是UTC时间,查看Flink日志,发现输出时间比当前时间晚8个小时. 通过如下命令,调整成北京时间 cp /usr/share/zoneinfo/Asia/S ...
- MyEclipse中好用的快捷键汇总
MyEclipse中常用的快捷键有很多,合理的使用其中一些快捷键组合,可以有效提高开发的效率和质量. 1.Ctrl + Shift + R:打开资源.可以查找并打开工作区中任何一个文件,且支持使用通配 ...
- bzoj4326 运输计划
4326: NOIP2015 运输计划 Time Limit: 30 Sec Memory Limit: 128 MB Description 公元 2044 年,人类进入了宇宙纪元.L 国有 n ...