当前位置:网站首页>Leetcode - 380 o (1) time to insert, delete and get random elements (design hash table + array)
Leetcode - 380 o (1) time to insert, delete and get random elements (design hash table + array)
2022-07-25 15:40:00 【Cute at the age of three @d】


Hashtable +list


class RandomizedSet {
// Put the value in list in In order to random access
private List<Integer> list;
//map Of key Is the value map Of val yes Values in list Position in
// For convenience in o(1) Within time Insert delete value
private Map<Integer,Integer> map;
public RandomizedSet() {
list = new ArrayList<>();
map = new HashMap<>();
}
public boolean insert(int val) {
// If the value already exists
if(map.containsKey(val) == true)
return false;
else{
// Newly added value index
int index = list.size();
// Added to the list
list.add(val);
// Added to the map
map.put(val,index);
return true;
}
}
public boolean remove(int val) {
//val non-existent Delete failed
if(map.containsKey(val) == false)
return false;
else{
//list The subscript of the last number
int lasti = list.size()-1;
//list The value of the last number
int lastv = list.get(lasti);
//val stay list Subscript in
int vali = map.get(val);
// take list The last number of is placed in val stay list Location
list.set(vali,lastv);
// modify list The last number of is map Subscript in
map.put(lastv,vali);
// Delete list The last number in
list.remove(lasti);
// stay map Delete in val
map.remove(val);
return true;
}
}
public int getRandom() {
int n = list.size();
// produce 0 - n-1 The random integer of the range
return list.get((int)(Math.random()*n));
}
}
/** * Your RandomizedSet object will be instantiated and called as such: * RandomizedSet obj = new RandomizedSet(); * boolean param_1 = obj.insert(val); * boolean param_2 = obj.remove(val); * int param_3 = obj.getRandom(); */
边栏推荐
- Qtime定义(手工废物利用简单好看)
- Hdu3873 shortest path with dependency (topological sorting)
- LeetCode - 622 设计循环队列 (设计)
- Deadlock gossip
- C#精挑整理知识要点10 泛型(建议收藏)
- LeetCode - 677 键值映射(设计)*
- PAT甲级1151 LCA in a Binary Tree (30 分)
- 《图书馆管理系统——“借书还书”模块》项目研发阶段性总结
- LeetCode - 380 O(1) 时间插入、删除和获取随机元素 (设计 哈希表+数组)
- LeetCode - 641 设计循环双端队列(设计)*
猜你喜欢

《图书馆管理系统——“借书还书”模块》项目研发阶段性总结

Geogle Colab笔记1--运行Geogle云端硬盘上的.py文件

GAMES101复习:线性代数

4PAM在高斯信道与瑞利信道下的基带仿真系统实验

获取键盘按下的键位对应ask码

MySQL - user and permission control

MySQL—用户和权限管控

解决vender-base.66c6fc1c0b393478adf7.js:6 TypeError: Cannot read property ‘validate‘ of undefined问题

小波变换--dwt2 与wavedec2

No tracked branch configured for branch xxx or the branch doesn‘t exist. To make your branch trac
随机推荐
How to solve cross domain problems
PAT甲级1153 Decode Registration Card of PAT (25 分)
2019 Shaanxi provincial competition j-bit operation + greed
Wechat applet
CF750F1-思维dp
CVPR 2022 | in depth study of batch normalized estimation offset in network
死锁杂谈
window系统黑窗口redis报错20Creating Server TCP listening socket *:6379: listen: Unknown error19-07-28
Pytorch学习笔记--Pytorch常用函数总结1
Games101 review: 3D transformation
GAMES101复习:变换
使用cpolar建立一个商业网站(如何购买域名)
C # carefully sorting out key points of knowledge 11 entrustment and events (recommended Collection)
2021 Shanghai match-b-ranked DP
HDU3873-有依赖的最短路(拓扑排序)
Take you to create your first C program (recommended Collection)
Brain racking CPU context switching
Games101 review: linear algebra
I want to ask whether the variable configuration function can only be used in SQL mode
带你详细认识JS基础语法(建议收藏)