数组如何实现O(1)的删除元素

使用以下数据结构:

  • 动态数组存储元素值
  • 哈希表存储存储值到索引的映射。

删除任意索引元素需要线性时间,这里的解决方案是总是删除最后一个元素。

  • 将要删除元素和最后一个元素交换。
  • 将最后一个元素删除。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
type RandomizedSet struct {
    // 存储元素的值
    nums []int
    // 记录每个元素对应在 nums 中的索引
    valToIndex map[int]int
}
 
// 构造方法
func Constructor() RandomizedSet {
    return RandomizedSet{
        nums:       []int{},
        valToIndex: map[int]int{},
    }
}
 
// 如果 val 不存在集合中,则插入并返回 true,否则直接返回 false
func (this *RandomizedSet) Insert(val int) bool {
    // 若 val 已存在,不用再插入
    if _, ok := this.valToIndex[val]; ok {
        return false
    }
    // 若 val 不存在,插入到 nums 尾部,
    // 并记录 val 对应的索引值
    this.nums = append(this.nums, val)
    this.valToIndex[val] = len(this.nums) - 1
    return true
}
 
// 如果 val 在集合中,则删除并返回 true,否则直接返回 false
func (this *RandomizedSet) Remove(val int) bool {
    // 若 val 不存在,不用再删除
    if _, ok := this.valToIndex[val]; !ok {
        return false
    }
    // 拿到待删除 val 的索引
    index := this.valToIndex[val]
    // 最后一个元素的索引
    lastIndex := len(this.nums) - 1
    // 将最后一个元素对应的索引修改为 index
    this.valToIndex[this.nums[lastIndex]] = index
    // 交换 val 和最后一个元素 (将 val 索引位置设为最后一个值也可)
    this.nums[index], this.nums[lastIndex] = this.nums[lastIndex], this.nums[index] //this.nums[index] = this.nums[lastIndex]
    // 将数组中最后一个值 val 删除
    this.nums = this.nums[0 : len(this.nums)-1]
    // 删除元素 val 对应的索引
    delete(this.valToIndex, val)
    return true
}
 
// 从集合中等概率地随机获得一个元素
func (this *RandomizedSet) GetRandom() int {
    // 随机获取 nums 中的一个元素
    return this.nums[rand.Intn(len(this.nums))]
}

  

posted @   ☞@_@  阅读(102)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· 单线程的Redis速度为什么快?
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
点击右上角即可分享
微信分享提示