-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path295. 数据流的中位数
63 lines (59 loc) · 1.46 KB
/
295. 数据流的中位数
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
//295. 数据流的中位数
class MedianFinder {
public:
/** initialize your data structure here. */
MedianFinder() {
}
void addNum(int num) {
auto m=small.size(),n=big.size();
if(small.empty()){
small.push(num);
return;
}
if(m==n){
auto s=small.top(),b=big.top();
if(s<num)
big.push(num);
else small.push(num);
}
else if(m>n){
if(small.top()<num)
big.push(num);
else
{
auto t=small.top();
small.pop();
small.push(num);
big.push(t);
}
}
else if(m<n){
if(big.top()>num)
small.push(num);
else
{
auto t=big.top();
small.push(t);
big.pop();
big.push(num);
}
}
}
double findMedian() {
auto m=small.size(),n=big.size();
if(m==n)
return (small.top()+big.top())/2.0;
else if(m>n)
return small.top();
return big.top();
}
private:
priority_queue<int>small;
priority_queue<int,vector<int>,greater<int>>big;
};
/**
* Your MedianFinder object will be instantiated and called as such:
* MedianFinder obj = new MedianFinder();
* obj.addNum(num);
* double param_2 = obj.findMedian();
*/