Skip to content

Commit cc05b5b

Browse files
committed
feat: add cpp solution to lc problem: No.0295.Find Median from Data Stream
1 parent f9c501d commit cc05b5b

File tree

3 files changed

+110
-1
lines changed

3 files changed

+110
-1
lines changed

solution/0200-0299/0295.Find Median from Data Stream/README.md

+39-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
<pre>addNum(1)
2727
addNum(2)
2828
findMedian() -&gt; 1.5
29-
addNum(3)
29+
addNum(3)
3030
findMedian() -&gt; 2</pre>
3131

3232
<p><strong>进阶:</strong></p>
@@ -117,6 +117,44 @@ class MedianFinder {
117117
*/
118118
```
119119

120+
### **C++**
121+
122+
```cpp
123+
class MedianFinder {
124+
public:
125+
/** initialize your data structure here. */
126+
MedianFinder() {}
127+
128+
void addNum(int num) {
129+
max_heap.push(num);
130+
min_heap.push(max_heap.top());
131+
max_heap.pop();
132+
if (min_heap.size() > max_heap.size()) {
133+
max_heap.push(min_heap.top());
134+
min_heap.pop();
135+
}
136+
}
137+
138+
double findMedian() {
139+
if (max_heap.size() > min_heap.size()) {
140+
return max_heap.top();
141+
}
142+
return (double)(max_heap.top() + min_heap.top()) / 2;
143+
}
144+
145+
private:
146+
priority_queue<int> max_heap;
147+
priority_queue<int, vector<int>, greater<int>> min_heap;
148+
};
149+
150+
/**
151+
* Your MedianFinder object will be instantiated and called as such:
152+
* MedianFinder* obj = new MedianFinder();
153+
* obj->addNum(num);
154+
* double param_2 = obj->findMedian();
155+
*/
156+
```
157+
120158
### **...**
121159

122160
```

solution/0200-0299/0295.Find Median from Data Stream/README_EN.md

+38
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,44 @@ class MedianFinder {
126126
*/
127127
```
128128

129+
### **Go**
130+
131+
```cpp
132+
class MedianFinder {
133+
public:
134+
/** initialize your data structure here. */
135+
MedianFinder() {}
136+
137+
void addNum(int num) {
138+
max_heap.push(num);
139+
min_heap.push(max_heap.top());
140+
max_heap.pop();
141+
if (min_heap.size() > max_heap.size()) {
142+
max_heap.push(min_heap.top());
143+
min_heap.pop();
144+
}
145+
}
146+
147+
double findMedian() {
148+
if (max_heap.size() > min_heap.size()) {
149+
return max_heap.top();
150+
}
151+
return (double)(max_heap.top() + min_heap.top()) / 2;
152+
}
153+
154+
private:
155+
priority_queue<int> max_heap;
156+
priority_queue<int, vector<int>, greater<int>> min_heap;
157+
};
158+
159+
/**
160+
* Your MedianFinder object will be instantiated and called as such:
161+
* MedianFinder* obj = new MedianFinder();
162+
* obj->addNum(num);
163+
* double param_2 = obj->findMedian();
164+
*/
165+
```
166+
129167
### **...**
130168

131169
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
class MedianFinder {
2+
public:
3+
/** initialize your data structure here. */
4+
MedianFinder() {}
5+
6+
void addNum(int num) {
7+
max_heap.push(num);
8+
min_heap.push(max_heap.top());
9+
max_heap.pop();
10+
if (min_heap.size() > max_heap.size()) {
11+
max_heap.push(min_heap.top());
12+
min_heap.pop();
13+
}
14+
}
15+
16+
double findMedian() {
17+
if (max_heap.size() > min_heap.size()) {
18+
return max_heap.top();
19+
}
20+
return (double)(max_heap.top() + min_heap.top()) / 2;
21+
}
22+
23+
private:
24+
priority_queue<int> max_heap;
25+
priority_queue<int, vector<int>, greater<int>> min_heap;
26+
};
27+
28+
/**
29+
* Your MedianFinder object will be instantiated and called as such:
30+
* MedianFinder* obj = new MedianFinder();
31+
* obj->addNum(num);
32+
* double param_2 = obj->findMedian();
33+
*/

0 commit comments

Comments
 (0)