From c65b8097a593243e664b37a7837074bcee7d4bf6 Mon Sep 17 00:00:00 2001 From: Powfu <1875065753@qq.com> Date: Wed, 27 Dec 2023 10:52:10 +0000 Subject: [PATCH] =?UTF-8?q?add=202101040022/chapter=5F9/=E4=BB=A5=E5=B8=B8?= =?UTF-8?q?=E6=95=B0=E6=97=B6=E9=97=B4=E6=8F=92=E5=85=A5=E5=88=A0=E9=99=A4?= =?UTF-8?q?=E8=8E=B7=E5=8F=96=E9=9A=8F=E6=9C=BA=E5=85=83=E7=B4=A0.cpp.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Powfu <1875065753@qq.com> --- ...7\346\234\272\345\205\203\347\264\240.cpp" | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 "2101040022/chapter_9/\344\273\245\345\270\270\346\225\260\346\227\266\351\227\264\346\217\222\345\205\245\345\210\240\351\231\244\350\216\267\345\217\226\351\232\217\346\234\272\345\205\203\347\264\240.cpp" diff --git "a/2101040022/chapter_9/\344\273\245\345\270\270\346\225\260\346\227\266\351\227\264\346\217\222\345\205\245\345\210\240\351\231\244\350\216\267\345\217\226\351\232\217\346\234\272\345\205\203\347\264\240.cpp" "b/2101040022/chapter_9/\344\273\245\345\270\270\346\225\260\346\227\266\351\227\264\346\217\222\345\205\245\345\210\240\351\231\244\350\216\267\345\217\226\351\232\217\346\234\272\345\205\203\347\264\240.cpp" new file mode 100644 index 00000000..c4a2639c --- /dev/null +++ "b/2101040022/chapter_9/\344\273\245\345\270\270\346\225\260\346\227\266\351\227\264\346\217\222\345\205\245\345\210\240\351\231\244\350\216\267\345\217\226\351\232\217\346\234\272\345\205\203\347\264\240.cpp" @@ -0,0 +1,33 @@ +class RandomizedSet { +public: + vector nums; + unordered_map indexMap; + RandomizedSet() { + + } + + bool insert(int val) { + if(indexMap.count(val)){ + return false; + } + indexMap[val] = nums.size(); + nums.push_back(val); + return true; + } + + bool remove(int val) { + if(!indexMap.count(val)){ + return false; + } + int index = indexMap[val]; + indexMap[nums.back()]=index; + swap(nums.back(),nums[index]); + nums.pop_back(); + indexMap.erase(val); + return true; + } + + int getRandom() { + return nums[rand()%nums.size()]; + } +}; \ No newline at end of file -- Gitee