-
Notifications
You must be signed in to change notification settings - Fork 266
Expand file tree
/
Copy path41.java
More file actions
34 lines (32 loc) · 1.04 KB
/
41.java
File metadata and controls
34 lines (32 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
class MedianFinder {
PriorityQueue<Integer> heapS;
PriorityQueue<Integer> heapL;
/** initialize your data structure here. */
public MedianFinder() {
heapS = new PriorityQueue<>();
heapL = new PriorityQueue<>(new Comparator<Integer>(){
public int compare(Integer o1, Integer o2){
return o2 - o1;
}
});
}
public void addNum(int num) {
//当heapS为空或者heapS和heapL的size一样时, 优先向heapS中添加元素
if(heapS.isEmpty() || heapS.size()==heapL.size()){
if(!heapL.isEmpty() && num < heapL.peek()){
heapL.add(num);
num = heapL.poll();
}
heapS.add(num);
}else{
if(!heapS.isEmpty() && num > heapS.peek()){
heapS.add(num);
num = heapS.poll();
}
heapL.add(num);
}
}
public double findMedian() {
return heapS.size()>heapL.size()? heapS.peek() : (heapL.peek() + heapS.peek())/2.0;
}
}