1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-10-21 21:16:45 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
This commit is contained in:
2022-05-02 23:44:12 +08:00
parent 7ea03594b3
commit 2a71c78585
4790 changed files with 11696 additions and 10944 deletions

View File

@@ -12,7 +12,7 @@
"translatedContent": "<p><code>RandomizedCollection</code> 是一种包含数字集合(可能是重复的)的数据结构。它应该支持插入和删除特定元素,以及删除随机元素。</p>\n\n<p>实现 <code>RandomizedCollection</code> 类:</p>\n\n<ul>\n\t<li><code>RandomizedCollection()</code>初始化空的 <code>RandomizedCollection</code> 对象。</li>\n\t<li><code>bool insert(int val)</code>&nbsp;将一个 <code>val</code> 项插入到集合中,即使该项已经存在。如果该项不存在,则返回 <code>true</code> ,否则返回 <code>false</code> 。</li>\n\t<li><code>bool remove(int val)</code>&nbsp;如果存在,从集合中移除一个 <code>val</code> 项。如果该项存在,则返回 <code>true</code> ,否则返回 <code>false</code> 。注意,如果 <code>val</code> 在集合中出现多次,我们只删除其中一个。</li>\n\t<li><code>int getRandom()</code> 从当前的多个元素集合中返回一个随机元素。每个元素被返回的概率与集合中包含的相同值的数量 <strong>线性相关</strong> 。</li>\n</ul>\n\n<p>您必须实现类的函数,使每个函数的 <strong>平均</strong> 时间复杂度为 <code>O(1)</code> 。</p>\n\n<p><strong>注意:</strong>生成测试用例时,只有在 <code>RandomizedCollection</code> 中 <strong>至少有一项</strong> 时,才会调用 <code>getRandom</code> 。</p>\n\n<p>&nbsp;</p>\n\n<p><strong>示例 1:</strong></p>\n\n<pre>\n<strong>输入</strong>\n[\"RandomizedCollection\", \"insert\", \"insert\", \"insert\", \"getRandom\", \"remove\", \"getRandom\"]\n[[], [1], [1], [2], [], [1], []]\n<strong>输出</strong>\n[null, true, false, true, 2, true, 1]\n\n<strong>解释</strong>\nRandomizedCollection collection = new RandomizedCollection();// 初始化一个空的集合。\ncollection.insert(1);// 向集合中插入 1 。返回 true 表示集合不包含 1 。\ncollection.insert(1);// 向集合中插入另一个 1 。返回 false 表示集合包含 1 。集合现在包含 [1,1] 。\ncollection.insert(2);// 向集合中插入 2 ,返回 true 。集合现在包含 [1,1,2] 。\ncollection.getRandom();// getRandom 应当有 2/3 的概率返回 1 1/3 的概率返回 2 。\ncollection.remove(1);// 从集合中删除 1 ,返回 true 。集合现在包含 [1,2] 。\ncollection.getRandom();// getRandom 应有相同概率返回 1 和 2 。\n</pre>\n\n<p>&nbsp;</p>\n\n<p><strong>提示:</strong></p>\n\n<ul>\n\t<li><code>-2<sup>31</sup>&nbsp;&lt;= val &lt;= 2<sup>31</sup>&nbsp;- 1</code></li>\n\t<li><code>insert</code>,&nbsp;<code>remove</code>&nbsp;和&nbsp;<code>getRandom</code>&nbsp;最多 <strong>总共</strong> 被调用&nbsp;<code>2 * 10<sup>5</sup></code>&nbsp;次</li>\n\t<li>当调用 <code>getRandom</code> 时,数据结构中 <strong>至少有一个</strong> 元素</li>\n</ul>\n",
"isPaidOnly": false,
"difficulty": "Hard",
"likes": 241,
"likes": 243,
"dislikes": 0,
"isLiked": null,
"similarQuestions": "[{\"title\": \"Insert Delete GetRandom O(1)\", \"titleSlug\": \"insert-delete-getrandom-o1\", \"difficulty\": \"Medium\", \"translatedTitle\": \"O(1) \\u65f6\\u95f4\\u63d2\\u5165\\u3001\\u5220\\u9664\\u548c\\u83b7\\u53d6\\u968f\\u673a\\u5143\\u7d20\"}]",
@@ -161,7 +161,7 @@
"__typename": "CodeSnippetNode"
}
],
"stats": "{\"totalAccepted\": \"22.7K\", \"totalSubmission\": \"52.2K\", \"totalAcceptedRaw\": 22691, \"totalSubmissionRaw\": 52160, \"acRate\": \"43.5%\"}",
"stats": "{\"totalAccepted\": \"23.2K\", \"totalSubmission\": \"53.5K\", \"totalAcceptedRaw\": 23211, \"totalSubmissionRaw\": 53535, \"acRate\": \"43.4%\"}",
"hints": [],
"solution": null,
"status": null,