1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-01-26 02:00:27 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
leetcode-problemset/leetcode-cn/problem (Chinese)/数据流中的中位数 [shu-ju-liu-zhong-de-zhong-wei-shu-lcof].html

41 lines
1.4 KiB
HTML
Raw Normal View History

2023-12-09 18:42:21 +08:00
<p><strong>中位数&nbsp;</strong>是有序整数列表中的中间值。如果列表的大小是偶数,则没有中间值,中位数是两个中间值的平均值。</p>
2022-03-27 20:38:29 +08:00
2023-12-09 18:42:21 +08:00
<p>例如,<br />
<code>[2,3,4]</code> 的中位数是 <code>3</code><br />
<code>[2,3]</code> 的中位数是 <code>(2 + 3) / 2 = 2.5</code><br />
设计一个支持以下两种操作的数据结构:</p>
2022-03-27 20:38:29 +08:00
<ul>
2023-12-09 18:42:21 +08:00
<li><code>void addNum(int num)</code> - 从数据流中添加一个整数到数据结构中。</li>
<li><code>double findMedian()</code> - 返回目前所有元素的中位数。</li>
2022-03-27 20:38:29 +08:00
</ul>
<p><strong>示例 1</strong></p>
2023-12-09 18:42:21 +08:00
<pre>
<strong>输入:
</strong>["MedianFinder","addNum","addNum","findMedian","addNum","findMedian"]
2022-03-27 20:38:29 +08:00
[[],[1],[2],[],[3],[]]
<strong>输出:</strong>[null,null,null,1.50000,null,2.00000]
</pre>
<p><strong>示例 2</strong></p>
2023-12-09 18:42:21 +08:00
<pre>
<strong>输入:
</strong>["MedianFinder","addNum","findMedian","addNum","findMedian"]
2022-03-27 20:38:29 +08:00
[[],[2],[],[3],[]]
<strong>输出:</strong>[null,null,2.00000,null,2.50000]</pre>
<p>&nbsp;</p>
2023-12-09 18:42:21 +08:00
<p><strong>提示:</strong></p>
2022-03-27 20:38:29 +08:00
<ul>
<li>最多会对&nbsp;<code>addNum、findMedian</code> 进行&nbsp;<code>50000</code>&nbsp;次调用。</li>
</ul>
<p>注意:本题与主站 295 题相同:<a href="https://leetcode-cn.com/problems/find-median-from-data-stream/">https://leetcode-cn.com/problems/find-median-from-data-stream/</a></p>
2023-12-09 18:42:21 +08:00
<p>&nbsp;</p>