java 295.从数据流中找到中位数(#)。java

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 295.从数据流中找到中位数(#)。java相关的知识,希望对你有一定的参考价值。

public class MedianFinder {
    PriorityQueue<Integer> min;
    PriorityQueue<Integer> max;
    /** initialize your data structure here. */
    public MedianFinder() {
        min=new  PriorityQueue<Integer>();
        max=new  PriorityQueue<Integer>(100,new Comparator<Integer>(){
            @Override
            public int compare(Integer a, Integer b){
                return b-a;
            }
        });
    }
    
    public void addNum(int num) {
        if(min.size()==0 && max.size()==0) max.offer(num);
        else if(max.size()!=0){
            if(num>max.peek())min.add(num);
            else max.add(num);
        }
        else{
            if(num<min.peek())max.add(num);
            else min.add(num);
        }
        if(max.size()-min.size()>1)min.add(max.poll());
        else if(min.size()-max.size()>1)max.add(min.poll());
    }
    
    public double findMedian() {
        if(min.size()==max.size())return (min.peek()+max.peek())/2.0;
        if(min.size()>max.size()) return min.peek();
        else return max.peek();
    }
}

/**
 * Your MedianFinder object will be instantiated and called as such:
 * MedianFinder obj = new MedianFinder();
 * obj.addNum(num);
 * double param_2 = obj.findMedian();
 */
class MedianFinder {

    private Queue<Long> small = new PriorityQueue(),
                        large = new PriorityQueue();

    public void addNum(int num) {
        large.add((long) num);
        small.add(-large.poll());
        if (large.size() < small.size())
            large.add(-small.poll());
    }

    public double findMedian() {
        return large.size() > small.size()
               ? large.peek()
               : (large.peek() - small.peek()) / 2.0;
    }
};
public class MedianFinder {
    
    private PriorityQueue<Integer> minHeap;
    private PriorityQueue<Integer> maxHeap;
    /** initialize your data structure here. */
    public MedianFinder() {
        minHeap = new PriorityQueue<>();
        maxHeap = new PriorityQueue<>(new Comparator<Integer>(){
            public int compare(Integer a, Integer b) {
                return b - a;
            }
        });
    }
    
    public void addNum(int num) {
        maxHeap.offer(num);
        minHeap.offer(maxHeap.poll());
        if (minHeap.size() - 1 > maxHeap.size()) {
            maxHeap.offer(minHeap.poll());
        }
    }
    
    public double findMedian() {
        return maxHeap.size() == minHeap.size() ? ( maxHeap.peek() + minHeap.peek())/2.0 : minHeap.peek();
    }
}

/**
 * Your MedianFinder object will be instantiated and called as such:
 * MedianFinder obj = new MedianFinder();
 * obj.addNum(num);
 * double param_2 = obj.findMedian();
 */

以上是关于java 295.从数据流中找到中位数(#)。java的主要内容,如果未能解决你的问题,请参考以下文章

java 295.从数据流中找到中位数(#)。java

java 295.从数据流中找到中位数(#)。java

java 295.从数据流中找到中位数(#)。java

java 295.从数据流中找到中位数(#)。java

java 295.从数据流中找到中位数(#)。java

java 295.从数据流中找到中位数(#)。java