leetcode295
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的更多相关文章
- [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 ...
- leetcode295 Find Median from Data Stream
""" Median is the middle value in an ordered integer list. If the size of the list is ...
随机推荐
- what's the difference between grouping and facet in lucene 3.5
I found in lucene 3.5 contrib folder two plugins: one is grouping, the other is facet. In my option ...
- 《Kubernetes权威指南》笔记-Pod、容器与Node的关系
- 廖雪峰Java6 IO编程-2input和output-4Filter模式
1.JDK提供的InputStream分为两类: 直接提供数据的InputStream * FileInputStream:从文件读取 * ServletInputStream:从HTTP请求读取数据 ...
- java 字节码 指令集 汇编(转)
https://blog.csdn.net/github_35983163/article/details/52945845 网上找的没有指令码这列 自己把它加上 更方便查阅 指令从0x00-0xc ...
- PHP——抽象类与接口的区别
在学习PHP面向对象时,都会在抽象类与接口上迷惑,作用差不多为什么还那么容易混淆,何不留一去一?但是事实上两者的区别还是很大的,如果能够很好地运用PHP的两个方法,面向对象的程序设计将会更加合理.清晰 ...
- centos6.5远程桌面连接(VNC\SPice)
在Linux下用vnc远程桌面,centos中默认没有安装VNC 查询系统是否安装VNC # rpm -q tigervnc tigervnc-server 安装VNC服务 # yum install ...
- nginx配置location总结及rewrite规则写法 (若配置reload或restart不生效就stop start)
location正则写法 一个示例: location = / { # 精确匹配 / ,主机名后面不能带任何字符串 [ configuration A ] } location / { # 因为所有的 ...
- 在线学习和在线凸优化(online learning and online convex optimization)—基础介绍1
开启一个在线学习和在线凸优化框架专题学习: 1.首先介绍在线学习的相关概念 在线学习是在一系列连续的回合(rounds)中进行的: 在回合,学习机(learner)被给一个question:(一个向量 ...
- mobilehack -转
# mobileHack##工具类网站 [HTML5 与 CSS3 技术应用评估](http://html5please.com/ "html5与css3技术应用评估") [各种奇 ...
- python3基础操作
ubuntu下python连接mysql apt-get install python-mysqldb 获取当前时间 >>> from datetime import datetim ...