题目:

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的更多相关文章

  1. 【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 ...

  2. Leetcode刷题C#版之 Length of Last Word

    题目: Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return t ...

  3. Leetcode刷题C#版之Toeplitz Matrix

    题目: Toeplitz Matrix A matrix is Toeplitz if every diagonal from top-left to bottom-right has the sam ...

  4. 【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 ...

  5. (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 ...

  6. LeetCode刷题总结-数组篇(中)

    本文接着上一篇文章<LeetCode刷题总结-数组篇(上)>,继续讲第二个常考问题:矩阵问题. 矩阵也可以称为二维数组.在LeetCode相关习题中,作者总结发现主要考点有:矩阵元素的遍历 ...

  7. C#LeetCode刷题-分治算法

    分治算法篇 # 题名 刷题 通过率 难度 4 两个排序数组的中位数 C#LeetCode刷题之#4-两个排序数组的中位数(Median of Two Sorted Arrays)-该题未达最优解 30 ...

  8. C#LeetCode刷题-二分查找​​​​​​​

    二分查找篇 # 题名 刷题 通过率 难度 4 两个排序数组的中位数 C#LeetCode刷题之#4-两个排序数组的中位数(Median of Two Sorted Arrays)-该题未达最优解 30 ...

  9. C#LeetCode刷题-数组

    数组篇 # 题名 刷题 通过率 难度 1 两数之和 C#LeetCode刷题之#1-两数之和(Two Sum) 43.1% 简单 4 两个排序数组的中位数 C#LeetCode刷题之#4-两个排序数组 ...

随机推荐

  1. Web Component总结

    Web Component 一个Web组件通常由四个部分组成:模板.Shadow DOM.自定义元素与打包,其中Shadow DOM解决了组件在页面中的封装问题 Shadow DOM 有shadow ...

  2. MLlib--PIC算法

    转载请标明出处http://www.cnblogs.com/haozhengfei/p/82c3ef86303321055eb10f7e100eb84b.html PIC算法   幂迭代聚类     ...

  3. addslashes() 函数返回在预定义字符之前添加反斜杠的字符串

    . 预定义字符是: 单引号(') 双引号(") 反斜杠(\) NULL 提示:该函数可用于为存储在数据库中的字符串以及数据库查询语句准备字符串. 注释:默认地,PHP 对所有的 GET.PO ...

  4. java访问修饰符 public protect default private

    适用范围<访问权限范围越小,安全性越高>   访问权限   类   包  子类  其他包   public     ok   ok    ok     ok         (对所有可用的 ...

  5. PostgreSQL 的 distinct on 的理解

    摘录自:http://www.cnblogs.com/gaojian/archive/2012/09/05/2671381.html 对于 select distinct on , 可以利用下面的例子 ...

  6. SSH的jar包下载地址

    spring http://repo.spring.io/libs-release-local/org/springframework/spring/ 条理清晰的搭建SSH环境之添加所需jar包 ht ...

  7. 关于ls命令的实例

    生活映射程序---------科技创造生活 ls 是Linux的常用命令之一直接使用 ls 命令的话只会列出对应的文件名ls -l 命令会显示文件和目录,包括文件类型,大小,修改日期和时间,权限信息等 ...

  8. Ubuntu 11.10 Server下搭建Maven私服

      安装Nexus服务的文档可以参考官方站点:http://www.sonatype.com/books/nexus-book/reference/install-sect-install.html ...

  9. Java数据结构和算法(十四)——堆

    在Java数据结构和算法(五)——队列中我们介绍了优先级队列,优先级队列是一种抽象数据类型(ADT),它提供了删除最大(或最小)关键字值的数据项的方法,插入数据项的方法,优先级队列可以用有序数组来实现 ...

  10. js面向对象之继承那点事儿根本就不是事

    继承 说道这个继承,了解object-oriented的朋友都知道,大多oo语言都有两种,一种是接口继承(只继承方法签名):一种是实现继承(继承实际的方法) 奈何js中没有签名,因而只有实现继承,而且 ...