题解 | #数据流中的中位数#
数据流中的中位数
http://www.nowcoder.com/practice/9be0172896bd43948f8a32fb954e1be1
public class Solution {
PriorityQueue<Integer> max = new PriorityQueue<>((a,b) ->(b-a)); //大根堆
PriorityQueue<Integer> min = new PriorityQueue<>(); //小根堆
public void Insert(Integer num) {
min.offer(num);
max.offer(min.poll());
if(max.size() > min.si***.offer(max.poll());
}
}
public Double GetMedian() {
if(max.si***.size()){
return (double)min.peek();
}else{
return (double)(max.peek() + min.peek())/2;
}
}
}
查看12道真题和解析