[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 elements are allowed.
insert(val)
: Inserts an item val to the collection.remove(val)
: Removes an item val from the collection if present.getRandom
: Returns a random element from current collection of elements. The probability of each element being returned is linearly related to the number of same value the collection contains.
Example:
// Init an empty collection.
RandomizedCollection collection = new RandomizedCollection(); // Inserts 1 to the collection. Returns true as the collection did not contain 1.
collection.insert(1); // Inserts another 1 to the collection. Returns false as the collection contained 1. Collection now contains [1,1].
collection.insert(1); // Inserts 2 to the collection, returns true. Collection now contains [1,1,2].
collection.insert(2); // getRandom should return 1 with the probability 2/3, and returns 2 with the probability 1/3.
collection.getRandom(); // Removes 1 from the collection, returns true. Collection now contains [1,2].
collection.remove(1); // getRandom should return 1 and 2 both equally likely.
collection.getRandom();
Solution1: HashMap + ArrayList
code
public class RandomizedCollection {
class Node {
public int value;
public int index;
public Node(int val, int idx) {
value = val;
index = idx;
}
} private Map<Integer, List<Integer>> map;
private List<Node> list;
private Random r; /** Initialize your data structure here. */
public RandomizedCollection() {
map = new HashMap<>();
list = new ArrayList<>();
r = new Random();
} /** Inserts a value to the collection. Returns true if the collection did not already contain the specified element. */
public boolean insert(int val) {
List<Integer> l = map.getOrDefault(val, new ArrayList<>());
l.add(list.size());
map.put(val, l);
list.add(new Node(val, l.size() - 1));
return l.size() == 1;
} /** Removes a value from the collection. Returns true if the collection contained the specified element. */
public boolean remove(int val) {
if (!map.containsKey(val)) return false;
List<Integer> l = map.get(val);
int removeIdx = l.get(l.size() - 1);
Node replaceNode = list.get(list.size() - 1); // deal with HashMap
map.get(replaceNode.value).set(replaceNode.index, removeIdx);
l.remove(l.size() - 1);
if (l.size() == 0) map.remove(val); // deal with List
list.set(removeIdx, replaceNode);
list.remove(list.size() - 1); return true;
} /** Get a random element from the collection. */
public int getRandom() {
return list.get(r.nextInt(list.size())).value;
}
}
[leetcode]381. Insert Delete GetRandom O(1) - Duplicates allowed常数时间插入删除取随机值的更多相关文章
- [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] 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 O(1) 时间插入、删除和获取随机元素 - 允许重复(C++/Java)
题目: Design a data structure that supports all following operations in averageO(1) time. Note: Duplic ...
- 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 (插入删除和获得随机数 常数时间 允许重复项)
Design a data structure that supports all following operations in average O(1) time. Note: Duplicate ...
- [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) 、381. Insert Delete GetRandom O(1) - Duplicates allowed
380. Insert Delete GetRandom O(1) 实现插入.删除.获得随机数功能,且时间复杂度都在O(1).实际上在插入.删除两个功能中都包含了查找功能,当然查找也必须是O(1). ...
- 381. Insert Delete GetRandom O(1) - Duplicates allowed
Design a data structure that supports all following operations in average O(1) time. Note: Duplicate ...
随机推荐
- ubuntu 16.04 LTS 安装 teamviewer 13
背景介绍 由于需要做现场的远程支持,经协商后在现场的服务器上安装TeamViewer 以便后续操作. 本来以为很简单的一件事,谁知却稍微费了一番周折 :( 记录下来,希望提醒自己的同时也希望能够帮到 ...
- mysq更新(六) 单表查询 多表查询
本节重点: 单表查询 语法: 一.单表查询的语法 SELECT 字段1,字段2... FROM 表名 WHERE 条件 GROUP BY field HAVING 筛选 ORDER BY fiel ...
- ORM创建 脚本运行
- 使用Larave5.6l提交POST请求出现The page has expired due to inactivity错误
使用Larave5.6l提交POST请求出现The page has expired due to inactivity错误 一般是由于没有添加 csrf造成的 在表单下面的 第一个行 添加如下代码即 ...
- 写下thinkphp5和thinkphp3.2的不同
只列出一些自己的直观感受 1 引入了命令行,估计来源是laravel,前阵子刚练手完laravel5.0的系统, 感觉thinkphp5的命令行和laravel的很像 2 引入了路由,来源估计也是la ...
- JAVA Spring 简单的配置和操作 ( 创建实体类, 配置XML文件, 调试 )
< 1 > 实体类 Person package java_spring.modle; /** * 一个实体类( Person ) */ public class Person { pri ...
- 你该知道的 TValue
你该知道的 TValue Represents a lightweight version of the Variant type. TValue is a data structure that c ...
- 安装vue.js
1. 下载node https://nodejs.org/dist/v8.11.2/node-v8.11.2-x64.msi 2. 查看npm版本 在cmd下输入命令:npm -v 如果低于3.0版 ...
- WINRAR 自解压脚本命令及变量
自解压脚本命令 Path=d:\ ;绝对路径 ;Path=.\在当前文件夹中创建 ;Path=在“Program Files”中创建 ;在当前文件夹创建,无语句 Setup=释放后运行 Presetu ...
- 简单ssh建立 (paramiko)
SSH为建立在应用层和传输层基础上的安全协议.SSH是目前较可靠,专为远程登录会话和其他网络服务提供安全性的协议.利用SSH协议可以有效防止远程管理过程中的信息泄露问题. import paramik ...