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)。
数组可以实现插入、删除、获得随机数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的更多相关文章
- 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 ...
- [LeetCode] 380. Insert Delete GetRandom O(1) 常数时间内插入删除和获得随机数
Design a data structure that supports all following operations in average O(1) time. insert(val): In ...
- [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 ...
- [leetcode]380. Insert Delete GetRandom O(1)常数时间插入删除取随机值
Design a data structure that supports all following operations in average O(1) time. insert(val): In ...
- [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 ...
- Leetcode 380. 常数时间插入、删除和获取随机元素
1.题目描述 设计一个支持在平均 时间复杂度 O(1) 下,执行以下操作的数据结构. insert(val):当元素 val 不存在时,向集合中插入该项. remove(val):元素 val 存在时 ...
- ListView控件的Insert、Edit和Delete功能(第二部分)
本系列文章将通过一个简单的实例,结合我自己使用ListView的情况,展示如何用ASP.NET 3.5 ListView控件进行基本的Insert.Edit和Delete操作. 系统要求: Windo ...
- ListView控件的Insert、Edit和Delete功能(第一部分)
摘自:http://blog.ashchan.com/archive/2007/08/28/listview-control-insert-edit-amp-delete-part-1aspx/ Li ...
- 关于mysql的update、delete、和insert into能否使用别名问题
在工作中遇到这样一个问题,就是mysql在insert into时能不能使用别名,大家会很奇怪为什么insert into使用别名呢?原因在于原来的项目中使用了user表,新项目要将user表拆分为u ...
随机推荐
- DateTimeFormatter LocalDateTime 工具类
import java.text.SimpleDateFormat; import java.time.Duration; import java.time.Instant; import java. ...
- 第五次个人作业——Alpha测试
这个作业属于哪个课程 课程链接 这个作业要求在哪里 作业要求 团队名称 巧克力王子与六个小矮人 一.测试项目博客地址 项目名 团队名 博客地址 项目发布地址 西柚排课王 西柚排课王 https://w ...
- acrobat 导出300dpi图片
文件-导出-图象-jpeg 设置 分辨率118.11像素/厘米
- k8s安装之nginx.yaml
这里两个nginx.一个是用来测试最简单的集群的. 另一个是用来作grafana,prometheus,dashboard前端安全展示的. 简单版 apiVersion: apps/v1 kind: ...
- python算法与数据结构-选择排序算法(33)
一.选择排序的介绍 选择排序(Selection sort)是一种简单直观的排序算法.首先在未排序序列中找到最小(大)元素,存放到排序序列的起始位置,然后,再从剩余未排序元素中继续寻找最小(大)元素, ...
- Python语言程序设计(3)--数字类型及操作--实例3-天天向上的力量
1.整数 2.浮点数 3.复数 4.数值运算操作符 5.数值运算函数 5.天天向上的力量:实例
- 【学习笔记】Baby Step Giant Step算法及其扩展
1. 引入 Baby Step Giant Step算法(简称BSGS),用于求解形如\(a^x\equiv b\pmod p\)(\(a,b,p\in \mathbb{N}\))的同余方程,即著名的 ...
- HTML中的标签列表
1 :基础标签 <!DOCTYPE> :定义文档类型 <title>:定义文档标题 <h1>to<h2>定义HTML标题,其中h1到h6表示字体大小依次 ...
- Hello 2019【A,B,C】
#include<bits/stdc++.h> using namespace std; #define int long long signed main(){ string str; ...
- SaltStack 在 Windows 上的操作基础
SaltStack 在 windows上的操作基础 1.删除文件: salt '172.16.3.11' file.remove 'D:\downup\111.msu' 2.删除文件夹 salt '1 ...