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 ...
随机推荐
- 【分布式session】Spring-session的使用
概述 Session用于保存用户信息,通常一个Session保存一个用户信息,在以Tomcat为Servlet Container的web应用中,用户信息都保存在HttpSession中: 当用户发起 ...
- problem:为什么会有options请求
为了安全考虑,浏览器对资源访问有同源限制的问题,也就是web应用程序只能访问和它同一协议同一域名同一端口的web应用程序上的资源. 通过跨域资源共享机制可以让资源在浏览器中访问与该资源本身不同域的资源 ...
- vue请求拦截
写了很多的vue项目,经常碰到需要做请求拦截的情况,从发请求前的token判断到对返回信息的响应,我自己在不同的阶段是用不同的方式处理的. 入门阶段 记得当时做的第一个项目,是需要在请求头部加入登录是 ...
- H5移动端开发vue+vux
项目src中用到的npm包有(从编译打包到最终部署仍不能移除)1:vue 渐进式 JavaScript 框架 http://cn.vuejs.org/v2/guide/2: ...
- keystone认证服务
实验操作平台:OpenStack单节点操作 一.相关概念 1.认证(authentication) 认证是确认允许一个用户访问的进程 2.证书(credentials) 用于确认用户身份的数据 3.令 ...
- dell 7447加装SSD
老本加新件:) dell 7447第一款游匣? 14年冬入手,陪伴在下已有4年 一.需要拆机看接口(不知道的话),拆机无流程,网上一大把,此处不再赘述. 二.硬盘接口知识扩展: SATA3 m2接口有 ...
- WebSocket 启用压缩
m_client.Compression = CompressionMethod.Deflate;
- 01.String类字符串本质
String类是在java开发过程中,使用最最频繁的一个类,不管是 用户名 密码 还是http报文接收过来的数据,其本质就是字符序列 所以做为一个java开发者,我们要重点掌握好String的方法使用 ...
- Delphi获取其他exe程序版本号
delphi获取Exe文件版本信息的函数 Type TFileVersionInfo = Record FixedInfo:TVSFixedFileInfo; {版本信息} CompanyName:S ...
- C语言:冒泡排序
void sort(int arr[],int len) { ; ; i<len; i++) { printf("第%d轮:\n", i); // len-i+1:新轮比上轮 ...