1 Star 0 Fork 0

表情扭曲 / leetcode

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
lc295.java 1.09 KB
一键复制 编辑 原始数据 按行查看 历史
liu13 提交于 2019-03-01 21:08 . 20190301
package code;
import java.util.PriorityQueue;
/*
* 295. Find Median from Data Stream
* 题意:流数据中找中位数
* 难度:Hard
* 分类:
* 思路:用两个优先队列,一个保存左半边最大值,一个保存右半边最小值
* 保持左半边最多比右半边多一个数
* Tips:
*/
public class lc295 {
class MedianFinder {
PriorityQueue<Integer> pq1; //默认是最小,右半边
PriorityQueue<Integer> pq2; //左半边
/** initialize your data structure here. */
public MedianFinder() {
this.pq1 = new PriorityQueue();
this.pq2 = new PriorityQueue();
}
public void addNum(int num) {
pq1.add(num); //两个队列都过一遍
pq2.add(-pq1.poll());
if (pq1.size() < pq2.size()) //如果中位数是一个数,就存在左半边
pq1.add(-pq2.poll());
}
public double findMedian() {
if(pq1.size()==pq2.size()+1) return pq1.peek();
return -((double)(-pq1.peek()+pq2.peek()))/2;
}
}
}
1
https://gitee.com/abfantasy/leetcode.git
git@gitee.com:abfantasy/leetcode.git
abfantasy
leetcode
leetcode
master

搜索帮助

53164aa7 5694891 3bd8fe86 5694891