381. Insert Delete GetRandom O(1) - Duplicates allowed 發表於 2023-02-13 | 分類於 leetcode problemsolution123456789101112131415161718192021222324252627282930313233343536373839404142class RandomizedCollection {private: vector<int> nums; unordered_map<int, unordered_set<int>> val2idx;public: RandomizedCollection() { } bool insert(int val) { val2idx[val].insert(nums.size()); nums.push_back(val); return val2idx[val].size() ==1; } bool remove(int val) { if(val2idx[val].empty()) return false; int idx = *val2idx[val].begin(); val2idx[val].erase(idx); if(nums.size() -1 != idx){ int t = nums.back(); nums[idx] = t; val2idx[t].erase(nums.size()-1); val2idx[t].insert(idx); } nums.pop_back(); return true; } int getRandom() { return nums[rand()%nums.size()]; }};/** * Your RandomizedCollection object will be instantiated and called as such: * RandomizedCollection* obj = new RandomizedCollection(); * bool param_1 = obj->insert(val); * bool param_2 = obj->remove(val); * int param_3 = obj->getRandom(); */