1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-09-04 23:11:41 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
Files
leetcode-problemset/算法题(国内版)/problem (Chinese)/设计有序流 [design-an-ordered-stream].html

53 lines
2.4 KiB
HTML
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<p><code>n</code><code>(id, value)</code> 对,其中 <code>id</code><code>1</code><code>n</code> 之间的一个整数,<code>value</code> 是一个字符串。不存在 <code>id</code> 相同的两个 <code>(id, value)</code> 对。</p>
<p>设计一个流,以 <strong>任意</strong> 顺序获取 <code>n</code> 个 <code>(id, value)</code> 对,并在多次调用时 <strong><code>id</code> 递增的顺序</strong> 返回一些值。</p>
<p>实现 <code>OrderedStream</code> 类:</p>
<ul>
<li><code>OrderedStream(int n)</code> 构造一个能接收 <code>n</code> 个值的流,并将当前指针 <code>ptr</code> 设为 <code>1</code></li>
<li><code>String[] insert(int id, String value)</code> 向流中存储新的 <code>(id, value)</code> 对。存储后:
<ul>
<li>如果流存储有 <code>id = ptr</code><code>(id, value)</code> 对,则找出从 <code>id = ptr</code> 开始的 <strong>最长 id 连续递增序列</strong> ,并 <strong>按顺序</strong> 返回与这些 id 关联的值的列表。然后,将 <code>ptr</code> 更新为最后那个  <code>id + 1</code> 。</li>
<li>
<p>否则,返回一个空列表。</p>
</li>
</ul>
</li>
</ul>
<p> </p>
<p><strong>示例:</strong></p>
<p><strong><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2020/11/15/q1.gif" style="width: 682px; height: 240px;" /></strong></p>
<pre>
<strong>输入</strong>
["OrderedStream", "insert", "insert", "insert", "insert", "insert"]
[[5], [3, "ccccc"], [1, "aaaaa"], [2, "bbbbb"], [5, "eeeee"], [4, "ddddd"]]
<strong>输出</strong>
[null, [], ["aaaaa"], ["bbbbb", "ccccc"], [], ["ddddd", "eeeee"]]
<strong>解释</strong>
OrderedStream os= new OrderedStream(5);
os.insert(3, "ccccc"); // 插入 (3, "ccccc"),返回 []
os.insert(1, "aaaaa"); // 插入 (1, "aaaaa"),返回 ["aaaaa"]
os.insert(2, "bbbbb"); // 插入 (2, "bbbbb"),返回 ["bbbbb", "ccccc"]
os.insert(5, "eeeee"); // 插入 (5, "eeeee"),返回 []
os.insert(4, "ddddd"); // 插入 (4, "ddddd"),返回 ["ddddd", "eeeee"]
</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 <= n <= 1000</code></li>
<li><code>1 <= id <= n</code></li>
<li><code>value.length == 5</code></li>
<li><code>value</code> 仅由小写字母组成</li>
<li>每次调用 <code>insert</code> 都会使用一个唯一的 <code>id</code></li>
<li>恰好调用 <code>n</code><code>insert</code></li>
</ul>