public class MedianFinder
{
List<int> list = null;
int count = ;
/** initialize your data structure here. */
public MedianFinder()
{
this.list = new List<int>();
} public void AddNum(int num)
{
if (list.Count == )
{
list.Add(num);
}
else
{
if (num <= list[])
{
list.Insert(, num);
}
else if (num >= list[list.Count - ])
{
list.Add(num);
}
else
{
for (int i = ; i < list.Count; i++)
{
if (num >= list[i] && num <= list[i + ])
{
list.Insert(i + , num);
break;
}
}
}
}
count++;
} public double FindMedian()
{
var mid = count / ;
var re = count % ;
if (re == )
{
var a = mid - ;
var b = mid;
return Convert.ToDouble(list[a] + list[b]) / ;
}
else
{
return Convert.ToDouble(list[mid]);
}
}
}

leetcode295的更多相关文章

  1. [Swift]LeetCode295. 数据流的中位数 | Find Median from Data Stream

    Median is the middle value in an ordered integer list. If the size of the list is even, there is no ...

  2. leetcode295 Find Median from Data Stream

    """ Median is the middle value in an ordered integer list. If the size of the list is ...

随机推荐

  1. sklearn.cross_validation 0.18版本废弃警告及解决方法

    转载:cheneyshark 机器环境: scikit-learn==0.19.1 Python 2.7.13 train_test_split基本用法 在机器学习中,我们通常将原始数据按照比例分割为 ...

  2. Java 内存溢出(java.lang.OutOfMemoryError)情况总结

    最近做一个项目,因为分了十几个模块,但是每次在Eclipse中启动Tomcat必须加载四五个模块,这样出现了 java.lang.OutOfMemoryError 原因是Eclipse中Tomcat设 ...

  3. NIO框架之MINA源码解析(五):NIO超级陷阱和使用同步IO与MINA通信

    1.NIO超级陷阱 之所以说NIO超级陷阱,就是因为我在本系列开头的那句话,因为使用缺陷导致客户业务系统瘫痪.当然,我对这个问题进行了很深的追踪,包括对MINA源码的深入了解,但其实之所以会出现这个问 ...

  4. 廖雪峰Java1-3流程控制-3条件判断

    1.if条件判断的格式 if (条件) { 代码块 } if (条件) { 代码块1 } else { 代码块2 } if (条件1) { 代码块1 } else if { 代码块2 } else { ...

  5. HDOJ 2001 计算两点间的距离

    #include<iostream> #include<cstdio> using namespace std; int main() { double a, b, x, y; ...

  6. SSH配置文件详解

    SSH:是一种安全通道协议,主要用来实现字符界面的远程登录,远程复制等功能. 在RHEL系统中SSH使用的是OpenSSH服务器,由opensh,openssh-server等软件包提供的. sshd ...

  7. [TFS]TFS强制删除离职人员签出锁定项的方法

    步骤: 1.连接到TFS数据库服务器的tfsversioncontrol库: 2.查tbl_workspace表,找出那哥们的工作目录, 如select * from tbl_workspace wh ...

  8. CentOS安装nginx以及负载均衡的搭建

    依赖环境,没有安装的需要安装一下 yum install gcc yum install pcre-devel yum install zlib zlib-devel yum install open ...

  9. xcode pod install 安装失败,提示缺少文件

    I had the same problem in Xcode 6.1.1. I did the following to solve it: Set the configuration file s ...

  10. 插件开发-滑条(slide)开发

    自己一直很喜欢开发组件,只是OPP学的不是很精,自己在项目中用别人的框架进行项目开发,难免受制于人,也许这就是个人实际项目需求和框架提供的多少有点不符,引导我自己尝试开发一些自己常用的组件,话不多说, ...