380. Insert Delete GetRandom O(1)

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

数组可以实现插入、删除、获得随机数O(1),但查找就不行了。(当然对于数组,直接删除的时间复杂度不是O(1),因为可能需要移动)

hash、set都是红黑树实现的,查找、插入、删除的时间复杂度在O(logn)。

unordered_map、unordered_set是哈希表实现的,查找、插入、删除的时间复杂度在O(1)。但unordered_map、unordered_set都只能用迭代器访问,无法用索引访问,所以获得随机数的时间复杂度不是O(1)。但是unordered_map、unordered_set都还是有size()的函数,只是不像vector那样用size来获得index访问。

unordered_set用迭代器访问的方式如下:

#include <iostream>
#include <unordered_set> using namespace std; int main(){
unordered_set<int> s;
s.insert();
s.insert();
s.insert();
for(auto it = s.begin();it != s.end();it++)
cout << *it << endl;
}

注意:result.back()返回的是最后一个位置的数值,不是下标

      在删除的函数中,除了删除vector中存储的数值,还要删除map中数值与索引,不然下次访问还会有这个被删除的数字

vector存储数,unordered_map存储数和对应在vector的下标

class RandomizedSet {
public:
/** Initialize your data structure here. */
RandomizedSet() { } /** Inserts a value to the set. Returns true if the set did not already contain the specified element. */
bool insert(int val) {
if(m.find(val) != m.end())
return false;
result.push_back(val);
m[val] = result.size() - ;
return true;
} /** Removes a value from the set. Returns true if the set contained the specified element. */
bool remove(int val) {
if(m.find(val) == m.end())
return false;
int index = m[val];
result[index] = result.back();
m[result.back()] = index;
result.pop_back();
m.erase(val);
return true;
} /** Get a random element from the set. */
int getRandom() {
int index = rand() % result.size();
return result[index];
}
private:
vector<int> result;
unordered_map<int,int> m;
};

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

与380题不同,这个题允许重复

unordered_map存储的数和数对应存储索引的集合,用set存储

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) {
int index = result.size();
result.push_back(val);
m[val].insert(index);
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 index = *m[val].begin();
m[val].erase(index);
if(index != result.size() - ){
result[index] = result.back();
m[result.back()].erase(result.size() - );
m[result.back()].insert(index);
}
result.pop_back();
return true;
} /** Get a random element from the collection. */
int getRandom() {
if(result.empty())
return -;
int index = rand()%result.size();
return result[index];
}
private:
vector<int> result;
unordered_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 380. Insert Delete GetRandom O(1) 、381. Insert Delete GetRandom O(1) - Duplicates allowed的更多相关文章

  1. 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 ...

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

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

  3. [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 ...

  4. [leetcode]380. Insert Delete GetRandom O(1)常数时间插入删除取随机值

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

  5. [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 ...

  6. Leetcode 380. 常数时间插入、删除和获取随机元素

    1.题目描述 设计一个支持在平均 时间复杂度 O(1) 下,执行以下操作的数据结构. insert(val):当元素 val 不存在时,向集合中插入该项. remove(val):元素 val 存在时 ...

  7. ListView控件的Insert、Edit和Delete功能(第二部分)

    本系列文章将通过一个简单的实例,结合我自己使用ListView的情况,展示如何用ASP.NET 3.5 ListView控件进行基本的Insert.Edit和Delete操作. 系统要求: Windo ...

  8. ListView控件的Insert、Edit和Delete功能(第一部分)

    摘自:http://blog.ashchan.com/archive/2007/08/28/listview-control-insert-edit-amp-delete-part-1aspx/ Li ...

  9. 关于mysql的update、delete、和insert into能否使用别名问题

    在工作中遇到这样一个问题,就是mysql在insert into时能不能使用别名,大家会很奇怪为什么insert into使用别名呢?原因在于原来的项目中使用了user表,新项目要将user表拆分为u ...

随机推荐

  1. 安装 docker-compose 配置 lnmp

    1.安装docker-compose 确保已经安装了docker sudo curl -L "https://github.com/docker/compose/releases/downl ...

  2. 使用Numpy的矩阵来实现神经网络

    要是书都讲得这么细致, AI也不会那么难学啦. import numpy as np # sigmoid作为隐藏层的激活函数 def sigmoid(x): return 1 / (1 + np.ex ...

  3. XML-1

    1.什么是XML xml即 Extensible Markup Language,中文叫可扩展标记语言,是一种具有结构性的标记语言. 2.Xml文档的构成 XML文档即用xml语言编写的文档,它包括以 ...

  4. 怎么给win10进行分区?

    新安装的win10系统的朋友,对于win10系统分区不满意应该如何是好呢?今天给大家介绍两种win10系统分区的方法,一个是windows自带分区管理软件来操作;另一个就是简单方便的分区助手来帮助您进 ...

  5. 大数据之路week07--day07 (Sqoop 从mysql增量导入到HDFS)

    我们之前导入的都是全量导入,一次性全部导入,但是实际开发并不是这样,例如web端进行用户注册,mysql就增加了一条数据,但是HDFS中的数据并没有进行更新,但是又再全部导入一次又完全没有必要. 所以 ...

  6. php 常用字符串函数总结

    php里面自带的字符串函数,日期函数,数组函数等,有时候可以帮助我们解决很复杂的问题,运用起来也比较简单. 下面总结了一下常用的字符串函数. addcslashes — 为字符串里面的部分字符添加反斜 ...

  7. c#基础用法

    1.注释符 1)注销 2)解释 2.3种方式 1)单行注释 // 2)多行注释 /*要注释的内容*/ 3)文档注释 /// 多用来解释类或方法 3.数据类型 1)值类型 2)引用类型 1.对象 obj ...

  8. dedecms列表页使用noflag

    最近小编使用dedecms遇到列表页需要使用noflag,在网上找了一圈都是直接替换代码,试用了一下并不能解决问题. 以下是小编自己根据资料整理的...多说一句由于各个编辑器打开的方式可能代码不在这一 ...

  9. Windows异常处理机制简介

    windows系统里,为了保证系统内核的强壮和稳定,为了保证用户程序的强壮和稳定,提供了异常处理机制,来帮助程序员和系统使用人员处理异常.简单来说,当CPU执行代码时,发生异常,会把异常告知操作系统, ...

  10. zeebe 集成elasticsearch exporter && 添加operate

    zeebe 的operate是一个功能比较强大的管理工具,比simple-monitor 有好多方面上的改进 安全,支持用户账户的登陆 界面更友好,界面比较符合开团队工作流引擎的界面 系统监控更加强大 ...