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. ssh调试及指定私钥

    1.ssh调试 ssh -vT username@ip[or hostname] T表示测试,v显示详细信息 也可以配置config文件(在~/.ssh/config)指定用户名和密码 如 [gerr ...

  2. Lucene 4.8 - Facet Demo

    package com.fox.facet; /* * Licensed to the Apache Software Foundation (ASF) under one or more * con ...

  3. js中两个==和三个===的区别

    首先,== equality 等同,=== identity 恒等. ==, 两边值类型不同的时候,要先进行类型转换,再比较. ==,不做类型转换,类型不同的一定不等. 下面分别说明: 先说 ===, ...

  4. 《Java并发编程实战》笔记-synchronized和ReentrantLock

    在一些内置锁无法满足需求的情况下,ReentrantLock可以作为一种高级工具.当震要一些高级功能时才应该使用ReentrantLock,这些功能包括:可定时的.可轮询的与可中断的锁获取操作,公平队 ...

  5. 客户端负载均衡Feign之四:Feign配置

    Ribbon配置 在Feign中配置Ribbon非常简单,直接在application.properties中配置即可,如: # 设置连接超时时间 ribbon.ConnectTimeout=500 ...

  6. NIO文件锁FileLock

    目录 <linux文件锁flock> <NIO文件锁FileLock> <java程序怎么在一个电脑上只启动一次,只开一个进程> 文件锁可以是shared(共享锁) ...

  7. jquery插件:textarea的字数提示、textBox的文字提示

    引用文件:    <script src=”/TextTip/TextTip.js” type=”text/javascript”></script> 一.textarea的字 ...

  8. lamda匿名函数(与sorted(),filter(),map() 一起用), 递归函数, 二分查找

    一. 匿名函数 为了解决一些简单的需求而设计的一句话函数.  lambda表示的是匿名函数. 不需要用def来声明, 一句话就可以声明出一个函数 语法:    函数名 = lambda 参数: 返回值 ...

  9. linux下打开文件、编辑文本cat\gedit\nano

    cat: 文本编辑器:gedit.nano,要获得根权限,在前面加上sudo

  10. Kafka参数详解

    一.相关参数配置 System 系统参数 #唯一标识在集群中的ID,要求是正数. broker.id=0 #服务端口,默认9092 port=9092 #监听地址,不设为所有地址 host.name= ...