insert-delete-getrandom-o1-duplicates-allowed
https://leetcode.com/problems/insert-delete-getrandom-o1-duplicates-allowed/
public class RandomizedCollection {
ArrayList<Integer> nums;
HashMap<Integer, Set<Integer>> locs;
java.util.Random rand;
/** Initialize your data structure here. */
public RandomizedCollection() {
rand = new java.util.Random();
nums = new ArrayList<Integer>();
locs = new HashMap<Integer, Set<Integer>>();
}
/** Inserts a value to the collection. Returns true if the collection did not already contain the specified element. */
public boolean insert(int val) {
boolean contained = locs.containsKey(val);
if (!contained) {
locs.put(val, new LinkedHashSet<Integer>());
}
locs.get(val).add(nums.size());
nums.add(val);
return !contained;
}
/** Removes a value from the collection. Returns true if the collection contained the specified element. */
public boolean remove(int val) {
boolean contained = locs.containsKey(val);
if (!contained) {
return false;
}
int index = locs.get(val).iterator().next();
locs.get(val).remove(index);
if (index < nums.size()-1) {
int change = nums.get(nums.size()-1);
nums.set(index, change);
locs.get(change).remove(nums.size()-1);
locs.get(change).add(index);
}
nums.remove(nums.size()-1);
if (locs.get(val).isEmpty()) {
locs.remove(val);
}
return true;
}
/** Get a random element from the collection. */
public int getRandom() {
return nums.get(rand.nextInt(nums.size()));
}
}
/**
* Your RandomizedCollection object will be instantiated and called as such:
* RandomizedCollection obj = new RandomizedCollection();
* boolean param_1 = obj.insert(val);
* boolean param_2 = obj.remove(val);
* int param_3 = obj.getRandom();
*/
insert-delete-getrandom-o1-duplicates-allowed的更多相关文章
- 381. Insert Delete GetRandom O(1) - Duplicates allowed
Design a data structure that supports all following operations in average O(1) time. Note: Duplicate ...
- 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). ...
- [LeetCode] Insert Delete GetRandom O(1) - Duplicates allowed 常数时间内插入删除和获得随机数 - 允许重复
Design a data structure that supports all following operations in average O(1) time. Note: Duplicate ...
- [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 ...
- [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 381. Insert Delete GetRandom O(1) - Duplicates allowed
原题链接在这里:https://leetcode.com/problems/insert-delete-getrandom-o1-duplicates-allowed/?tab=Description ...
- 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] 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) 常数时间内插入删除和获得随机数
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 ...
随机推荐
- vector 邻接表的建立(好笨啊,才懂,可能太困了吧)。。
原创,未经允许不得转载. 图的建立有两种,邻接矩阵和邻接表. 邻接矩阵适用于图较为密集,(稀疏图太浪费存储空间了),图如果较为稀疏,则使用邻接表为宜,dijkstra算法就是以邻接表为基础的. 有向无 ...
- sql中的if()和ifnull() 的用法和区别
if() 把salary表中的女改成男,男改成女: update salary set sex = if( sex = '男','女','男'); if(true,a,b), if(false,a, ...
- Bunch 转换为 HDF5 文件:高效存储 Cifar 等数据集
关于如何将数据集封装为 Bunch 可参考 关于 『AI 专属数据库的定制』的改进. PyTables 是 Python 与 HDF5 数据库/文件标准的结合.它专门为优化 I/O 操作的性能.最大限 ...
- 同步 Visual Studio Code 的设置与插件
工具推荐:Settings Sync. 小心有坑! VS Code 没有账号系统,所以设置不能同步,在多终端使用时不是很方便. 有一款插件能做这个事情:Settings Sync - Visual S ...
- 如何快速取消svn的关联
我们很多文件和文件项目都是通过svn从服务器上下载的,但是有些时候,出于某些目的,我们不想让这个文件继续和svn服务器进行关联,我们就需要解除这个关联,我观察了网上的相关方法,居然还有忽悠我们用reg ...
- opencv 掩膜操作 滤波 卷积核定义 增强对比度 掩膜运算
/* 矩阵的掩膜操作 0 掩膜mask 在这里进行增强对比度: [ [ 0,-1, 0 ], [-1, 5, -1], [ 0,-1, 0 ] ] 使用mask滑动图片每一个位置,进行卷积运算 这里这 ...
- JS AngualrJs 指令
本文基于 AngularJs 1.x 版本 内置指令 布尔属性 根据HTML标准的定义,布尔属性代表一个 true 或 false 值. 当这个属性出现时,这个属性的值就是 true (无论实际定义的 ...
- codevs 1462 素数和
1462 素数和 时间限制: 1 s 空间限制: 64000 KB 题目等级 : 青铜 Bronze 题目描述 Description 给定2个整数a,b 求出它们之间(不含a,b)所有 ...
- Splay和LCT的复杂度分析
\(Splay\)的复杂度分析 不论插入,删除还是访问,我们可以发现它们的复杂度都和\(splay\)操作的复杂度同阶,只是一点常数的区别 我们不妨假设有\(n\)个点的\(splay\),进行了\( ...
- 【漏洞预警】CVE-2017-8464 震网三代漏洞复现
早在6月13日,微软发布补丁修复编号为CVE-2017-8464的漏洞,本地用户或远程攻击者可以利用该漏洞生成特制的快捷方式,并通过可移动设备或者远程共享的方式导致远程代码执行,追溯到以前,NSA就承 ...