当前位置:网站首页>如何用常数时间插入、删除和获取随机元素
如何用常数时间插入、删除和获取随机元素
2022-07-16 06:04:00 【GreyZeng】
如何用常数时间插入、删除和获取随机元素
作者:Grey
原文地址: 如何用常数时间插入、删除和获取随机元素
题目链接
LeetCode 380. Insert Delete GetRandom O(1)
主要思路
因为要三个操作都达到O(1)时间复杂度,所以,我们可以空间换时间,采用两个哈希表来实现
Map<Integer,Integer> indexMap = new HashMap<>();
Map<Integer, Integer> valueMap = new HashMap<>();
这两个哈希表用于存储值和位置关系,比如,初始状态下,两个表都是空的,现在增加一个元素v,那么我们就把这个元素放到0号位置上,在哈希表结构中,就做如下操作
indexMap.put(v,0);
valueMap.put(0,v);
接下来来了一个元素x,我们就把这个元素x放到1号位置上,在哈希表的结构中,做如下操作
indexMap.put(x, 1);
valueMap.put(1, x);
这样,
通过indexMap就可以以时间复杂度O(1)找到某个元素是否存在,
通过valueMap就可以以时间复杂度O(1)找到某个位置的元素是什么。
同时,我们增加一个size变量来得到当前的元素一共有多少个,在我们调用getRandom()方法的时候,我们就可以通过
valueMap.get((int) (Math.random() * size));
以O(1)的时间复杂度,获取到随机位置的一个值。
最后是remove方法,由于我们可以获取到任何一个位置的值,同时也可以知道任何一个位置所代表的值是什么,我们可以很方便在indexMap和valueMap中删除掉对应的记录,
但是,被删除的位置就不是顺序排列的(会有缺口),举个例子,假设生成indexMap和valueMap如下

此时,如果要删掉2位置上的元素,被删除元素后的indexMap和valueMap如下

由于产生的缺口,就导致valueMap在随机取数的时候,如果随机的位置是缺口处,就无法拿到数据,所以,针对remove操作,我们每次操作完,都要把列表最后一个位置的数,去填补缺口,如上示例中,我们可以将4号位置的元素去填补空缺,填补后的indexMap和valueMap如下

保持表的顺序排列。
注意:在remove操作中,我们虽然是删除掉某个元素,并且用最后一个位置的元素去替换,但是我们的顺序必须是先取最后一个元素,并且用最后一个元素去覆盖要删除位置的元素,这样操作的目的是,防止删除的元素就是最后一个元素,导致用最后元素填充位置的时候,报空指针异常。
完整代码见
class RandomizedSet {
// 某个val在哪个位置
private Map<Integer, Integer> indexMap;
// 某个位置上的val是什么
private Map<Integer, Integer> valueMap;
private int size;
public RandomizedSet() {
size = 0;
indexMap = new HashMap<>();
valueMap = new HashMap<>();
}
public boolean insert(int val) {
if (!indexMap.containsKey(val)) {
valueMap.put(size, val);
indexMap.put(val, size++);
return true;
}
return false;
}
public boolean remove(int val) {
if (!indexMap.containsKey(val)) {
return false;
}
size--;
int removeIndex = indexMap.get(val);
int lastValue = valueMap.get(size);
valueMap.put(removeIndex, lastValue);
indexMap.put(lastValue, removeIndex);
indexMap.remove(val);
valueMap.remove(size);
return true;
}
public int getRandom() {
return valueMap.get((int) (Math.random() * size));
}
}
更多
边栏推荐
- 4-redis architecture design to usage scenario - redis request execution process
- 降级机制设计不当,线上系统瞬间崩溃...
- STM32 general timer
- nn. Gru() use
- 分库分表和 NewSQL 到底怎么选?
- C&W(Carlini & Wagner)
- One click VR panorama display
- 出海已成大势,技术如何赋能?| ArchSummit
- 我为 TDengine “带盐”!“高价”招募出镜开发者
- [C language note sharing] custom type: structure, enumeration, Union (recommended Collection)
猜你喜欢

Introduction to common controls of C form application

认识多银行资金系统(三)-------直联设置、信息权限和系统参数设置

Comparison and summary of five deep learning models for time series prediction: from simulated statistical model to unsupervised model that can be pre trained

Design of intelligent speech recognition Bluetooth headset based on wtk6900h speech recognition single chip

DataX extension vertica plug-in

Design of combustible gas smoke system based on single chip microcomputer (0489)
![[image recognition based on yolov5]](/img/bb/b91da2c63e60255d5c252ec12e7b93.png)
[image recognition based on yolov5]

合宙Air820ug昇級固件要點

Educational Codeforces Round 131 A - D

High cost analysis
随机推荐
合宙Air820ug升级固件要点
HMS Core图形图像技术展现最新功能和应用场景,加速构建数智生活
基于单片机的可燃气烟雾系统设计(#0489)
11. Container with the most water
Know the multi bank fund system (IV) -- plan tasks and account fund monitoring
C&W(Carlini & Wagner)
Shell脚本中的变量
出海已成大势,技术如何赋能?| ArchSummit
Kotlin | 为 Kotlin 编译器任务推出构建报告
Simple function relation
用训练集去重构权重空间的几何形态
[image recognition based on yolov5]
Liquibase first use
第一章 环境配置
[每周一更]-(第3期):Web开发安全注意事项
Construction binaire kubernets
XGBoostError: [10:19:14] C:\dev\libs\xgboost\src\objective\objective.cc:23:
Summary of ES interview questions - > Chapter 6
STM32 USB virtual serial port communication
Codeforces Global Round 21 B. NIT Destroys the Universe