380. O(1) 时间插入、删除和获取随机元素
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/b355b857dbbbfe481103ea6ca3fa74b0.png)
方法
class RandomizedSet {
public:
RandomizedSet() {
srand((unsigned)time(NULL));
}
bool insert(int val) {
if(indices.count(val)){
return false;
}
int index = nums.size();
nums.emplace_back(val);
indices[val] = index;
return true;
}
bool remove(int val) {
if(!indices.count(val)){
return false;
}
int index = indices[val];
int last = nums.back();
nums[index] = last;
indices[last] = index;
nums.pop_back();
indices.erase(val);
return true;
}
int getRandom() {
int randomIndex = rand()%nums.size();
return nums[randomIndex];
}
private:
vector<int> nums;
unordered_map<int,int> indices;
};