-
Notifications
You must be signed in to change notification settings - Fork 7
/
295.cpp
46 lines (43 loc) · 1.33 KB
/
295.cpp
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
35
36
37
38
39
40
41
42
43
44
45
46
class MedianFinder {
public:
/** initialize your data structure here. */
MedianFinder() : total(0) , small() , large() {
}
void addNum(int num) {
if(total % 2 == 0){//total为偶数时,压入的是第奇数个元素
if(!small.empty() && small.top() < num){
small.push(num);
num = small.top();
small.pop();
}
large.push(num);
}
else{ //total为奇数时,压入的是第偶数个元素
if(!large.empty() && large.top() > num){
large.push(num);
num = large.top();
large.pop();
}
small.push(num);
}
total++;
}
double findMedian() {
if(total == 0) return 0;
if(total % 2 == 1){
return large.top();
}
else
return static_cast<double>(large.top() + small.top()) / 2;
}
private:
int total = 0;
priority_queue<int,vector<int>,greater<int>> small; //存放较大的一半元素
priority_queue<int,vector<int>,less<int>> large; //存放较小的一半元素
};
/**
* Your MedianFinder object will be instantiated and called as such:
* MedianFinder obj = new MedianFinder();
* obj.addNum(num);
* double param_2 = obj.findMedian();
*/