每天学习一点算法 2026/04/06
题目:常数时间插入、删除和获取随机元素
实现RandomizedSet 类:
RandomizedSet() 初始化 RandomizedSet 对象
bool insert(int val) 当元素 val 不存在时,向集合中插入该项,并返回 true ;否则,返回 false 。
bool remove(int val) 当元素 val 存在时,从集合中移除该项,并返回 true ;否则,返回 false 。
int getRandom() 随机返回现有集合中的一项(测试用例保证调用此方法时集合中至少存在一个元素)。每个元素应该有 相同的概率 被返回。
你必须实现类的所有函数,并满足每个函数的 平均 时间复杂度为 O(1) 。
这个题还是比较简单的,关键在于如何实现 时间复杂度为 O(1) 的 getRandom()
数组可以轻松实现 O(1) 的 getRandom() 但是 insert() 和 remove()就不行了,需要遍历查找
我用 Set 可以是实现 O(1) 的 insert() 和 remove(),但是 getRandom() 如果要返回随即元素就必须得遍历,因为 Set 不能直接通过下标取值。
那么我们只能维护一个 数组 使用 Map 来记录元素的对应下标 ,注意我们删除的时候需要将最后一项放在删除下标出,然后再删除最后一项,这样就可以保证其他元素的下标不变。
typescript
class RandomizedSet {
map: Map<number, number> // Map<元素, 下标>
arr: number[] // 存放元素的数组
constructor() {
// 初始化
this.map = new Map()
this.arr = []
}
insert(val: number): boolean {
if (this.map.has(val)) {
return false
} else {
// 插入元素
this.map.set(val, this.arr.length)
this.arr.push(val)
return true
}
}
remove(val: number): boolean {
if (this.map.has(val)) {
// 删除元素
const index = this.map.get(val)
// 用末尾元素替换目标元素下标处的值
this.arr[index] = this.arr[this.arr.length - 1]
// 删除末尾元素
this.arr.pop()
// 更新之前末尾元素的下标
this.map.set(this.arr[index], index)
// 删除目标元素
this.map.delete(val)
return true
} else {
return false
}
}
getRandom(): number {
// 返回随机值
const random = Math.floor(Math.random() * this.arr.length)
return this.arr[random]
}
}
题目来源:力扣(LeetCode)