{"id":318,"date":"2025-06-30T22:43:26","date_gmt":"2025-06-30T14:43:26","guid":{"rendered":"http:\/\/getzs.online\/?p=318"},"modified":"2025-06-30T22:43:26","modified_gmt":"2025-06-30T14:43:26","slug":"380-o1-%e6%97%b6%e9%97%b4%e6%8f%92%e5%85%a5%e3%80%81%e5%88%a0%e9%99%a4%e5%92%8c%e8%8e%b7%e5%8f%96%e9%9a%8f%e6%9c%ba%e5%85%83%e7%b4%a0","status":"publish","type":"post","link":"https:\/\/hello.getzs.online\/index.php\/2025\/06\/30\/380-o1-%e6%97%b6%e9%97%b4%e6%8f%92%e5%85%a5%e3%80%81%e5%88%a0%e9%99%a4%e5%92%8c%e8%8e%b7%e5%8f%96%e9%9a%8f%e6%9c%ba%e5%85%83%e7%b4%a0\/","title":{"rendered":"380. O(1) \u65f6\u95f4\u63d2\u5165\u3001\u5220\u9664\u548c\u83b7\u53d6\u968f\u673a\u5143\u7d20"},"content":{"rendered":"\n<p><a href=\"https:\/\/leetcode.cn\/problems\/insert-delete-getrandom-o1\/\">380. O(1) \u65f6\u95f4\u63d2\u5165\u3001\u5220\u9664\u548c\u83b7\u53d6\u968f\u673a\u5143\u7d20<\/a><\/p>\n\n\n\n<p>\u5b9e\u73b0<code>RandomizedSet<\/code>&nbsp;\u7c7b\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>RandomizedSet()<\/code>\u00a0\u521d\u59cb\u5316\u00a0<code>RandomizedSet<\/code>\u00a0\u5bf9\u8c61<\/li>\n\n\n\n<li><code>bool insert(int val)<\/code>\u00a0\u5f53\u5143\u7d20\u00a0<code>val<\/code>\u00a0\u4e0d\u5b58\u5728\u65f6\uff0c\u5411\u96c6\u5408\u4e2d\u63d2\u5165\u8be5\u9879\uff0c\u5e76\u8fd4\u56de\u00a0<code>true<\/code>\u00a0\uff1b\u5426\u5219\uff0c\u8fd4\u56de\u00a0<code>false<\/code>\u00a0\u3002<\/li>\n\n\n\n<li><code>bool remove(int val)<\/code>\u00a0\u5f53\u5143\u7d20\u00a0<code>val<\/code>\u00a0\u5b58\u5728\u65f6\uff0c\u4ece\u96c6\u5408\u4e2d\u79fb\u9664\u8be5\u9879\uff0c\u5e76\u8fd4\u56de\u00a0<code>true<\/code>\u00a0\uff1b\u5426\u5219\uff0c\u8fd4\u56de\u00a0<code>false<\/code>\u00a0\u3002<\/li>\n\n\n\n<li><code>int getRandom()<\/code>\u00a0\u968f\u673a\u8fd4\u56de\u73b0\u6709\u96c6\u5408\u4e2d\u7684\u4e00\u9879\uff08\u6d4b\u8bd5\u7528\u4f8b\u4fdd\u8bc1\u8c03\u7528\u6b64\u65b9\u6cd5\u65f6\u96c6\u5408\u4e2d\u81f3\u5c11\u5b58\u5728\u4e00\u4e2a\u5143\u7d20\uff09\u3002\u6bcf\u4e2a\u5143\u7d20\u5e94\u8be5\u6709\u00a0<strong>\u76f8\u540c\u7684\u6982\u7387<\/strong>\u00a0\u88ab\u8fd4\u56de\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u4f60\u5fc5\u987b\u5b9e\u73b0\u7c7b\u7684\u6240\u6709\u51fd\u6570\uff0c\u5e76\u6ee1\u8db3\u6bcf\u4e2a\u51fd\u6570\u7684&nbsp;<strong>\u5e73\u5747<\/strong>&nbsp;\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a&nbsp;<code>O(1)<\/code>&nbsp;\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>\u8f93\u5165<\/strong>\n[\"RandomizedSet\", \"insert\", \"remove\", \"insert\", \"getRandom\", \"remove\", \"insert\", \"getRandom\"]\n[[], [1], [2], [2], [], [1], [2], []]\n<strong>\u8f93\u51fa<\/strong><\/pre>\n\n\n<p>[null, true, false, true, 2, true, false, 2]<\/p>\n\n\n\n<p><strong>\u89e3\u91ca<\/strong> RandomizedSet randomizedSet = new RandomizedSet(); randomizedSet.insert(1); \/\/ \u5411\u96c6\u5408\u4e2d\u63d2\u5165 1 \u3002\u8fd4\u56de true \u8868\u793a 1 \u88ab\u6210\u529f\u5730\u63d2\u5165\u3002 randomizedSet.remove(2); \/\/ \u8fd4\u56de false \uff0c\u8868\u793a\u96c6\u5408\u4e2d\u4e0d\u5b58\u5728 2 \u3002 randomizedSet.insert(2); \/\/ \u5411\u96c6\u5408\u4e2d\u63d2\u5165 2 \u3002\u8fd4\u56de true \u3002\u96c6\u5408\u73b0\u5728\u5305\u542b [1,2] \u3002 randomizedSet.getRandom(); \/\/ getRandom \u5e94\u968f\u673a\u8fd4\u56de 1 \u6216 2 \u3002 randomizedSet.remove(1); \/\/ \u4ece\u96c6\u5408\u4e2d\u79fb\u9664 1 \uff0c\u8fd4\u56de true \u3002\u96c6\u5408\u73b0\u5728\u5305\u542b [2] \u3002 randomizedSet.insert(2); \/\/ 2 \u5df2\u5728\u96c6\u5408\u4e2d\uff0c\u6240\u4ee5\u8fd4\u56de false \u3002 randomizedSet.getRandom(); \/\/ \u7531\u4e8e 2 \u662f\u96c6\u5408\u4e2d\u552f\u4e00\u7684\u6570\u5b57\uff0cgetRandom \u603b\u662f\u8fd4\u56de 2 \u3002<\/p>\n\n\n\n<p><strong>\u63d0\u793a\uff1a<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>-2<sup>31<\/sup> &lt;= val &lt;= 2<sup>31<\/sup> - 1<\/code><\/li>\n\n\n\n<li>\u6700\u591a\u8c03\u7528\u00a0<code>insert<\/code>\u3001<code>remove<\/code>\u00a0\u548c\u00a0<code>getRandom<\/code>\u00a0\u51fd\u6570\u00a0<code>2 *\u00a0<\/code><code>10<sup>5<\/sup><\/code>\u00a0\u6b21<\/li>\n\n\n\n<li>\u5728\u8c03\u7528\u00a0<code>getRandom<\/code>\u00a0\u65b9\u6cd5\u65f6\uff0c\u6570\u636e\u7ed3\u6784\u4e2d\u00a0<strong>\u81f3\u5c11\u5b58\u5728\u4e00\u4e2a<\/strong>\u00a0\u5143\u7d20\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>    \/**\n     * \u6a21\u62df\n     * 24 ms\n     *\/\n\n    \/\/ \u5b58\u50a8\u503c\u548c\u7d22\u5f15\n    public HashMap&lt;Integer, Integer> map;\n\n    \/\/ \u5b58\u50a8\u503c\n    public int&#91;]nums;\n\n    \/\/ \u6570\u7ec4\u957f\u5ea6\n    public int length;\n\n    \/\/ \u968f\u673a\u6570\n    Random random = new Random();\n\n    public RandomizedSet() {\n        \/\/ \u521d\u59cb\u5316\n        map = new HashMap&lt;>();\n        nums = new int&#91;2 * (int)1e5 + 1];\n        length = 0;\n    }\n\n    public boolean insert(int val) {\n        if (map.containsKey(val)) {\n            return false;\n        }\n        \/\/ \u8bb0\u5f55\u4e0b\u6807\u548c\u503c\u5173\u7cfb\n        map.put(val, length + 1);\n        nums&#91;length++] = val;\n        return true;\n    }\n\n    public boolean remove(int val) {\n        if (!map.containsKey(val)) {\n            return false;\n        }\n        nums&#91;map.get(val)] = nums&#91;--length];\n        \/\/ \u5982\u679c\u76f8\u540c\u5219\u4e0d\u66f4\u65b0\uff0c\u4e0d\u76f8\u540c\u5219\u66f4\u65b0\u65b0\u503c\u5230\u5220\u9664\u7684\u7d22\u5f15\u4e0a\n        if (map.get(val) !=  length) {\n            map.put(nums&#91;map.get(val)], map.get(val));\n        }\n        map.remove(val);\n        return true;\n    }\n\n    public int getRandom() {\n        return nums&#91;random.nextInt(length)];\n    }\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>380. O(1) \u65f6\u95f4\u63d2\u5165\u3001\u5220\u9664\u548c\u83b7\u53d6\u968f\u673a\u5143\u7d20 \u5b9e\u73b0RandomizedSet&nbsp;\u7c7b\uff1a \u4f60\u5fc5\u987b\u5b9e\u73b0\u7c7b [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7,11],"tags":[],"class_list":["post-318","post","type-post","status-publish","format-standard","hentry","category-leetcode"],"_links":{"self":[{"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/posts\/318","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/comments?post=318"}],"version-history":[{"count":1,"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/posts\/318\/revisions"}],"predecessor-version":[{"id":319,"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/posts\/318\/revisions\/319"}],"wp:attachment":[{"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/media?parent=318"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/categories?post=318"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/tags?post=318"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}