这道题中要求使用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. Linux 问题

    Loaded plugins: fastestmirror cd /etc/yum.repos.d mv CentOS-Base.repo CentOS-Base.repo.backup wget h ...

  2. Win10系列:C#应用控件进阶4

    多边形 若要绘制多边形需要用到Polygon元素,并通过定义一系列的点绘制多边形.Polygon类型的对象有Points属性, 这个属性用来定义组成边的点集.在前台代码中,使用空格分隔各个点,然后利用 ...

  3. JS数组映射详解

    现在这里占个坑位,免的忘了,需要整理一下最近的内容: 1.数组映射的使用 2.微信分享功能详解 3.jq自己封装 4.HTML的富文本应用

  4. MYSQL查询操作 详细

    学习目标 1 掌握select查询所有字段.指定字段的数据 2 掌握消除重复行命令distinct 3 掌握as给字段.表起别名 4 掌握条件查询where后跟比较运算符.逻辑运算符的用法 5 掌握条 ...

  5. 生成不同尺寸dimen的xml文件以及文件夹

    import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import j ...

  6. Forth 编译程序

    body, table{font-family: 微软雅黑; font-size: 13.5pt} table{border-collapse: collapse; border: solid gra ...

  7. windows环境下,spring boot服务使用docker打包成镜像并推送到云服务器私有仓库

    最近在淘宝上学习springcloud教程,其中有几节课是讲解讲本地springboot服务打包成镜像并推送到云服务器私有仓库,但是教程里面用的事Mac环境,我的是Windows环境,而且课程里面没有 ...

  8. generator mybatis逆向工程

    mybatis逆向工程:根据数据库中的表在项目中生成对应的 实体类,dao接口与mapper.xml映射文件 在eclipse中,以插件的形式的存在,把设置好的配置文件,直接通过eclipse中的插件 ...

  9. centos7 下zookeeper 部署 单机多实例模式

    centos7 下zookeeper 部署 本文参考https://www.linuxidc.com/Linux/2016-09/135052.htm 1.创建/usr/local/zookeeper ...

  10. Docker桥接宿主机网络与配置固定IP地址

    有些需求是把这个容器与宿主机在同一个网段,但是本人不建议这样子去操作,因为一个容器本身就是一个封装好的服务.建议去按默认的网络去实现. 临时设置 [root@linux-docker01 ~]# vi ...