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-两个排序数组 ...
随机推荐
- 如何开发由Create-React-App 引导的应用(二)
此文章是翻译How to develop apps bootstrapped with Create React App 官方文档 系列文章 如何开发由Create-React-App 引导的应用 如 ...
- dedecms 封面模板和列表模板有什么不同
封面模板,相当于你一个大栏目的封面.举例:你有一个栏目叫做"建站"而下面有很多子栏目,例如代码教程.模板下载.seo经验等,那么封面就相当于这个大栏目的首页,然后您可以在这个页面展 ...
- 给外行或者刚入门普及一下关于C#,.NET Framework(.NET框架),.Net,CLR,ASP,ASP.Net, VS,以及.NET Core的概念
一.概念 1. C# :C#是微软公司发布的一种面向对象的.运行于.NET Framework之上的高级程序设计语言. 2..NET Framework(.NET框架):.NET framework ...
- Android-第一天
1.google 2.application->application framework->libraries(调用关系) 3.strings.xml 是全局字符串的配置文件 4.ADT ...
- 顺序一致性内存模型与JMM的“顺序一致性”
顺序一致性内存模型是一个被计算机科学家理想化了的理论参考模型,它为程序员提供了极强的内存可见性保证.顺序一致性内存模型有两大特性.1)一个线程中的所有操作必须按照程序的顺序来执行.2)(不管程序是否同 ...
- 推荐一款强大的3D家装开源软件
2015年家装o2o着实火了一把.家装涉及到上门量尺,再设计,这个过程是免不了的. 目前基于bs架构的酷家乐,爱福窝等,流行起来就是着力于这点,通过一个点寻找突破口,进入深度挖掘,带动其他家具等产品来 ...
- 数据存储之HTTP Cookie
Cookie (HTTP Cookie) 作用 HTTP本身是无状态的,客户端通过Cookie来存储会话信息 限制 cookie在性质上是绑定在特定域名下的 意思是说当设定了一个cookie之后,再给 ...
- webpack+vuecli打包生成资源相对引用路径与背景图片的正确引用
资源相对引用路径 问题描述 一般情况下,通过webpack+vuecli默认打包的css.js等资源,路径都是绝对的. 但当部署到带有文件夹的项目中,这种绝对路径就会出现问题,因为把配置的static ...
- Servlet--取得session,application内置对象
在前面的博客里面,使用Servlet取得了request,response,config对象,实际上通过Servlet程序也可以取得session,application等内置对象. 1,通过Http ...
- 基于Elasticsearch搜索平台设计
背景 随着公司业务的高速发展以及数据爆炸式的增长,当前公司各产线都有关于搜索方面的需求,但是以前的搜索服务系统由于架构与业务上的设计,不能很好的满足各个业务线的期望,主要体现下面三个问题: 不能支持对 ...