[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 ...
随机推荐
- [UE4]为UStaticMeshComponent添加点击事件
BlockMesh->OnClicked.AddDynamic(this, &APuzzleBlock::BlockClicked); //鼠标点击事件 BlockMesh->On ...
- CA单向认证和双向认证的区别?
1:单向认证,内容会被串改吗?
- Oracle函数中将参数放在模糊查询中
--diagnosis_name like '%'||diagnosis_names||'%' create or replace function asdf(MIN_DATE IN varchar2 ...
- 【POJ】2420 A Star not a Tree?(模拟退火)
题目 传送门:QWQ 分析 军训完状态不好QwQ,做不动难题,于是就学了下模拟退火. 之前一直以为是个非常nb的东西,主要原因可能是差不多省选前我试着学一下但是根本看不懂? 骗分利器,但据说由于调参困 ...
- ExtJS模版技术
学习ExtJS一段时间以后,大家基本都会对于一些显示数据的组件不太符合需求,可能自己需要的组件在ExtJS里面不存在,这是大家基本就会使用Html属性,直接使用Html进行绘制页面数据展现. 但是,使 ...
- JavaScript中的闭包与匿名函数
知识内容: 1.预备知识 - 函数表达式 2.匿名函数 3.闭包 一.函数表达式 1.定义函数的两种方式 函数声明: 1 function func(arg0, arg1, arg2){ 2 // 函 ...
- mock单测
mockMvc执行流程总结: 整个过程:1.mockMvc.perform执行一个请求:2.MockMvcRequestBuilders.get("/user/1")构造一个请求3 ...
- mysql 数据字典
Information_schema数据库是MySQL自带的,它提供了访问数据库元数据的方式. desc tb_name ,describe tb_name,show columns from tb_ ...
- jpa-入门级测试
- HTML5 Canvas ( 绘制一轮弯月, 星空中的弯月 )
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...