题解 | #在二叉树中找到两个节点的最近公共祖先#
数据流中的中位数
http://www.nowcoder.com/practice/9be0172896bd43948f8a32fb954e1be1
import java.util.*; public class Solution{ PriorityQueue minHeap = new PriorityQueue<>(); PriorityQueue maxHeap = new PriorityQueue<>(new Comparator(){ @Override public int compare(Integer o1,Integer o2){ return o2 - o1; } }); int count = 0; public void Insert(Integer num){ if(count % 2 == 0){ maxHeap.offer(num); int temp = maxHeap.poll(); minHeap.offer(temp); }else{ minHeap.offer(num); int temp = minHeap.poll(); maxHeap.offer(temp); } count ++; } public Double GetMedian(){ if(count % 2 == 0){ return new Double(minHeap.peek() + maxHeap.peek()) / 2; }else{ return new Double(minHeap.peek()); } } }
我居南半坡 文章被收录于专栏
多刷题,积蓄力量,欢迎讨论