File tree 3 files changed +110
-1
lines changed
solution/0200-0299/0295.Find Median from Data Stream
3 files changed +110
-1
lines changed Original file line number Diff line number Diff line change 26
26
<pre >addNum(1)
27
27
addNum(2)
28
28
findMedian() -> ; 1.5
29
- addNum(3)
29
+ addNum(3)
30
30
findMedian() -> ; 2</pre >
31
31
32
32
<p ><strong >进阶:</strong ></p >
@@ -117,6 +117,44 @@ class MedianFinder {
117
117
*/
118
118
```
119
119
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
+
120
158
### ** ...**
121
159
122
160
```
Original file line number Diff line number Diff line change @@ -126,6 +126,44 @@ class MedianFinder {
126
126
*/
127
127
```
128
128
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
+
129
167
### ** ...**
130
168
131
169
```
Original file line number Diff line number Diff line change
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
+ */
You can’t perform that action at this time.
0 commit comments