数据流中得中位数
旋转数组的最小数字
http://www.nowcoder.com/questionTerminal/9f3231a991af4f55b95579b44b7a01ba
注意得点:
第一点:
import java.util.PriorityQueue;
public class Solution {
PriorityQueue<Integer> left=new PriorityQueue<>((n1,n2)->(n2-n1));////大根堆
PriorityQueue<Integer> right=new PriorityQueue<>((n1,n2)->(n1-n2));//小根堆
int n;
public void Insert(Integer num) {
left.add(num);
right.add(left.poll());
if(right.size()-left.size()>1){
left.add(right.poll());
}
}
public Double GetMedian() {
if(left.size()==right.size()){
return (double) (left.poll()+right.poll())/2.0;
}
return (double)right.poll();
}
}为什么不行?比如你插入1,2,3
第二点:只能用peek(),不能用poll()
return (double)right.peek();
}
return (double) (left.peek()+right.peek())/2.0;
腾讯音乐娱乐集团公司福利 283人发布
