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 ...
随机推荐
- pytest.2.运行多个文件
From: http://www.testclass.net/pytest/multiple_tests/ 背景 在现实的测试活动中,我们经常会定义多个用例文件,用来实现业务逻辑或其他逻辑上用例的物理 ...
- 测试教程网.unittest教程.2. 基本用法
From: http://www.testclass.net/pyunit/basic_example/ 我们通过最简单的例子来看一下unittest的基本用法,下面的代码测试了3个python字符串 ...
- ALGO-123_蓝桥杯_算法训练_A+B problem
问题描述 Given two integers A and B, your task is to output their sum, A+B. 输入格式 The input contains of o ...
- LeetCode——15. 3Sum
一.题目链接:https://leetcode.com/problems/3sum/ 二.题目大意: 3和问题是一个比较经典的问题,它可以看做是由2和问题(见http://www.cnblogs.co ...
- SpringMVC---400错误The request sent by the client was syntactically incorrect ()
在SpringMVC中使用@RequestBody和@ModelAttribute注解时遇到了很多问题,现记录下来. @ModelAttribute这个注解主要是将客户端请求的参数绑定参数到一个对象上 ...
- Java学习——多线程例子:李四王五
package cys; public class Example9_2 { public static void main(String[] args) { // TODO Auto-generat ...
- [ZZ]面向对象编程,再见!
面向对象编程,再见!- 机器学习算法与自然语言处理 https://mp.weixin.qq.com/s/icXBlVOOYLvDnER7cEeCeg https://medium.com/@csca ...
- 廖雪峰Java2面向对象编程-6Java核心类-5枚举类
直接定义常量 public class Weekday { //定义int常量 public static final int SUN = 0; public static final int MON ...
- 阿里云OSS图片云存储测试上传
在开发DEMO之前首先要确定 你开发OSS服务并获取了 accessKeyId和accessKeySecret final String key = MD5.Md5(DateFormat.format ...
- 基于线程池的多并发Socket程序的实现
Socket“服务器-客户端”模型的多线程并发实现效果的大体思路是:首先,在Server端建立“链接循环”,每一个链接都开启一个“线程”,使得每一个Client端都能通过已经建立好的线程来同时与Ser ...