Skip to content

Instantly share code, notes, and snippets.

@jianminchen
Created January 23, 2016 06:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jianminchen/31572a64b2af48e3ccce to your computer and use it in GitHub Desktop.
Save jianminchen/31572a64b2af48e3ccce to your computer and use it in GitHub Desktop.
class MedianFinder {
PriorityQueue<Integer> maxheap;
PriorityQueue<Integer> minheap;
public MedianFinder(){
// 新建最大堆
maxheap = new PriorityQueue<Integer>(11, new Comparator<Integer>(){
public int compare(Integer i1, Integer i2){
return i2 - i1;
}
});
// 新建最小堆
minheap = new PriorityQueue<Integer>();
}
// Adds a number into the data structure.
public void addNum(int num) {
// 如果最大堆为空,或者该数小于最大堆堆顶,则加入最大堆
if(maxheap.size() == 0 || num <= maxheap.peek()){
// 如果最大堆大小超过最小堆,则要平衡一下
if(maxheap.size() > minheap.size()){
minheap.offer(maxheap.poll());
}
maxheap.offer(num);
// 数字大于最小堆堆顶,加入最小堆的情况
} else if (minheap.size() == 0 || num > minheap.peek()){
if(minheap.size() > maxheap.size()){
maxheap.offer(minheap.poll());
}
minheap.offer(num);
// 数字在两个堆顶之间的情况
} else {
if(maxheap.size() <= minheap.size()){
maxheap.offer(num);
} else {
minheap.offer(num);
}
}
}
// Returns the median of current data stream
public double findMedian() {
// 返回大小较大的那个堆堆顶,如果大小一样说明是偶数个,则返回堆顶均值
if(maxheap.size() > minheap.size()){
return maxheap.peek();
} else if (maxheap.size() < minheap.size()){
return minheap.peek();
} else if (maxheap.isEmpty() && minheap.isEmpty()){
return 0;
} else {
return (maxheap.peek() + minheap.peek()) / 2.0;
}
}
};
简洁版
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment