这道题中要求使用O(1)的方法来删除和插入元素的,那么首先需要寻找到对应的元素,这个可以使用map的O(1)的查询时间的,然后是删除对应的元素的,那么可以根据 堆排序中类似的做法把最后面的元素插入到前面来并且置换掉对应的值的。

class RandomizedCollection {
public:
/** Initialize your data structure here. */
RandomizedCollection() { } /** Inserts a value to the collection. Returns true if the collection did not already contain the specified element. */
bool insert(int val) {
m[val].insert(num.size());
num.push_back(val);
return m[val].size()==;
} /** Removes a value from the collection. Returns true if the collection contained the specified element. */
bool remove(int val) {
if(m[val].empty()) return false;
int pos=*m[val].begin();
m[val].erase(pos);
if(pos!=num.size()-){
int temp=num.back();
num[pos]=temp;
m[temp].erase(num.size()-);
m[temp].insert(pos);
}
num.pop_back();
return true;
} /** Get a random element from the collection. */
int getRandom() {
return num[rand()%num.size()];
}
private:
vector<int> num;
map<int, set<int>> m;
}; /**
* 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();
*/

leetcode 381.Insert Delete GetRandom的更多相关文章

  1. [LeetCode] 381. Insert Delete GetRandom O(1) - Duplicates allowed 插入删除和获得随机数O(1)时间 - 允许重复

    Design a data structure that supports all following operations in average O(1) time. Note: Duplicate ...

  2. LeetCode 381. Insert Delete GetRandom O(1) - Duplicates allowed O(1) 时间插入、删除和获取随机元素 - 允许重复(C++/Java)

    题目: Design a data structure that supports all following operations in averageO(1) time. Note: Duplic ...

  3. [LeetCode] 381. Insert Delete GetRandom O(1) - Duplicates allowed 常数时间内插入删除和获得随机数 - 允许重复

    Design a data structure that supports all following operations in average O(1) time. Note: Duplicate ...

  4. LeetCode 381. Insert Delete GetRandom O(1) - Duplicates allowed

    原题链接在这里:https://leetcode.com/problems/insert-delete-getrandom-o1-duplicates-allowed/?tab=Description ...

  5. LeetCode 381. Insert Delete GetRandom O(1) - Duplicates allowed (插入删除和获得随机数 常数时间 允许重复项)

    Design a data structure that supports all following operations in average O(1) time. Note: Duplicate ...

  6. [leetcode]381. Insert Delete GetRandom O(1) - Duplicates allowed常数时间插入删除取随机值

    Design a data structure that supports all following operations in average O(1) time. Note: Duplicate ...

  7. [LeetCode] 380. Insert Delete GetRandom O(1) 插入删除获得随机数O(1)时间

    Design a data structure that supports all following operations in average O(1) time. insert(val): In ...

  8. leetcode 380. Insert Delete GetRandom O(1) 、381. Insert Delete GetRandom O(1) - Duplicates allowed

    380. Insert Delete GetRandom O(1) 实现插入.删除.获得随机数功能,且时间复杂度都在O(1).实际上在插入.删除两个功能中都包含了查找功能,当然查找也必须是O(1). ...

  9. [LeetCode] 380. Insert Delete GetRandom O(1) 常数时间内插入删除和获得随机数

    Design a data structure that supports all following operations in average O(1) time. insert(val): In ...

随机推荐

  1. Public_1.bat

    :: targetset targetFileName=Public_2set targetPath=./:: sourceset sourceFileName=Public_1@echo ui ru ...

  2. 安装vue 教程

    安装教程:https://www.cnblogs.com/zhaomeizi/p/8483597.html

  3. db2常用语句

    连接数据库 db2 connect to chmgmdb user ch_mgm 断开数据库 db2 disconnect current 查询 db2 "select * from btp ...

  4. 通读SCRUM实战指南教材,提出5个问题。

    问题一:为什么要制定优先级的排定和调整? 创建一个排好优先级的项目组合并讲重点放在转移团队上,如果做得对,可以消除在项目数量超过团队能力 的情况下过于常见的多任务处理. 问题二:为什么我们要做文档? ...

  5. idea使用错误及技巧总结合集(一)

    --- Cannot start process, the working directory 'E:\algorithm\algorithm' does not exist 1.点击run后再点击e ...

  6. C#连接Access数据库

    OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=XX/XXX ...

  7. 简要log4j配置

    若排版紊乱可查看我的个人博客原文地址 1. 配置效果 下面的log4j日志配置最终能达到的效果是对于全局日志可以根据级别输入到对应级别的日志文件中,注意低级别的日志文件会同时包含高级别的日志信息,例如 ...

  8. ReactiveCocoa的学习内容

    ReactiveCocoa • 知识备用库:iOS10 collectionView以前是复用的,但是iOS10以后就要设置一个属性才可以复用. • C语言面向过程编程,OC面向对象编程 • 链式编程 ...

  9. Markdown中特殊字符的转义字符

    上次在用Markdown记笔记时,当正文中写到<PROJECT>_<PATH>_<FILE>_H_时,<>里的内容显示显示不出来,就算用' '也显示不出 ...

  10. JavaScript学习摘要

    JavaScript的历史 1. css3在css2的基础上添加样式 可以做动画 也可以配合js操作2. h5在html4.01的基础上添加高级标签3. jQuery是JavaScript的封装4. ...