1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-01-11 02:58:13 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
leetcode-problemset/leetcode-cn/problem (Chinese)/设计跳表 [design-skiplist].html
2022-03-29 12:43:11 +08:00

56 lines
2.7 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<p>不使用任何库函数,设计一个 <strong>跳表</strong></p>
<p><strong>跳表</strong> 是在 <code>O(log(n))</code> 时间内完成增加、删除、搜索操作的数据结构。跳表相比于树堆与红黑树,其功能与性能相当,并且跳表的代码长度相较下更短,其设计思想与链表相似。</p>
<p>例如,一个跳表包含 <code>[30, 40, 50, 60, 70, 90]</code> ,然后增加 <code>80</code><code>45</code> 到跳表中,以下图的方式操作:</p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2019/09/27/1506_skiplist.gif" /><br />
<small>Artyom Kalinin [CC BY-SA 3.0], via <a href="https://commons.wikimedia.org/wiki/File:Skip_list_add_element-en.gif" target="_blank" title="Artyom Kalinin [CC BY-SA 3.0 (https://creativecommons.org/licenses/by-sa/3.0)], via Wikimedia Commons">Wikimedia Commons</a></small></p>
<p>跳表中有很多层,每一层是一个短的链表。在第一层的作用下,增加、删除和搜索操作的时间复杂度不超过 <code>O(n)</code>。跳表的每一个操作的平均时间复杂度是 <code>O(log(n))</code>,空间复杂度是 <code>O(n)</code></p>
<p>了解更多 :&nbsp;<a href="https://en.wikipedia.org/wiki/Skip_list" target="_blank">https://en.wikipedia.org/wiki/Skip_list</a></p>
<p>在本题中,你的设计应该要包含这些函数:</p>
<ul>
<li><code>bool search(int target)</code> : 返回target是否存在于跳表中。</li>
<li><code>void add(int num)</code>:&nbsp;插入一个元素到跳表。</li>
<li><code>bool erase(int num)</code>: 在跳表中删除一个值,如果&nbsp;<code>num</code>&nbsp;不存在直接返回false. 如果存在多个&nbsp;<code>num</code>&nbsp;,删除其中任意一个即可。</li>
</ul>
<p>注意,跳表中可能存在多个相同的值,你的代码需要处理这种情况。</p>
<p>&nbsp;</p>
<p><strong>示例 1:</strong></p>
<pre>
<b>输入</b>
["Skiplist", "add", "add", "add", "search", "add", "search", "erase", "erase", "search"]
[[], [1], [2], [3], [0], [4], [1], [0], [1], [1]]
<strong>输出</strong>
[null, null, null, null, false, null, true, false, true, false]
<strong>解释</strong>
Skiplist skiplist = new Skiplist();
skiplist.add(1);
skiplist.add(2);
skiplist.add(3);
skiplist.search(0); // 返回 false
skiplist.add(4);
skiplist.search(1); // 返回 true
skiplist.erase(0); // 返回 false0 不在跳表中
skiplist.erase(1); // 返回 true
skiplist.search(1); // 返回 false1 已被擦除
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>0 &lt;= num, target &lt;= 2 * 10<sup>4</sup></code></li>
<li>调用<code>search</code>, <code>add</code>, &nbsp;<code>erase</code>操作次数不大于&nbsp;<code>5 * 10<sup>4</sup></code>&nbsp;</li>
</ul>