Golang | Leetcode Golang题解之第381题O(1)时间插入、删除和获取随机元素-允许重复

news/2024/9/15 4:33:12 标签: Golang, Leetcode, 题解

题目:

题解

type RandomizedCollection struct {
    idx  map[int]map[int]struct{}
    nums []int
}

/** Initialize your data structure here. */
func Constructor() RandomizedCollection {
    return RandomizedCollection{
        idx: map[int]map[int]struct{}{},
    }
}

/** Inserts a value to the collection. Returns true if the collection did not already contain the specified element. */
func (r *RandomizedCollection) Insert(val int) bool {
    ids, has := r.idx[val]
    if !has {
        ids = map[int]struct{}{}
        r.idx[val] = ids
    }
    ids[len(r.nums)] = struct{}{}
    r.nums = append(r.nums, val)
    return !has
}

/** Removes a value from the collection. Returns true if the collection contained the specified element. */
func (r *RandomizedCollection) Remove(val int) bool {
    ids, has := r.idx[val]
    if !has {
        return false
    }
    var i int
    for id := range ids {
        i = id
        break
    }
    n := len(r.nums)
    r.nums[i] = r.nums[n-1]
    delete(ids, i)
    delete(r.idx[r.nums[i]], n-1)
    if i < n-1 {
        r.idx[r.nums[i]][i] = struct{}{}
    }
    if len(ids) == 0 {
        delete(r.idx, val)
    }
    r.nums = r.nums[:n-1]
    return true
}

/** Get a random element from the collection. */
func (r *RandomizedCollection) GetRandom() int {
    return r.nums[rand.Intn(len(r.nums))]
}

http://www.niftyadmin.cn/n/5635664.html

相关文章

【例003】利用MATLAB绘制有趣平面图形

题目&#xff1a; 用 ezplot 画出由方程 sin ⁡ ( x 2 m y 2 1000 ) cos ⁡ ( x y ) \sin(x^2\frac{my^2}{1000})\cos(xy) sin(x21000my2​)cos(xy) 确定隐函数的图形。 求解&#xff1a; 我们分别取m为100&#xff0c;1000,10000不同的值&#xff0c;绘制不同情况下的图…

kali——nikto的使用

目录 前言 使用方法 查看帮助&#xff08;--help&#xff09; 常规扫描&#xff08;-h&#xff09; 指定端口扫描&#xff08;-h -p&#xff09; 目录猜解&#xff08;-h -C&#xff09; 扫描敏感目录&#xff08;-h&#xff09; 保存扫描信息 前言 linux自带的nikto工…

sqlite3的db.parallelize方法:并行执行SQL语句,提升数据库操作效率

在Node.js环境中&#xff0c;sqlite3作为一个广受欢迎的轻量级数据库库&#xff0c;为开发者提供了一个与SQLite数据库进行交互的简洁API。在进行数据库操作时&#xff0c;为了提高效率&#xff0c;sqlite3提供了db.parallelize方法&#xff0c;允许并行执行多个SQL语句&#x…

c-数据结构(二叉搜索树)

树 概念 一组数据中除第一个节点&#xff08;第一个节点称为根节 点&#xff0c;没有直接前驱节点&#xff09;之外&#xff0c;其余任意节点有且仅有一个直接前驱&#xff0c;有零个或多个直接后继&#xff0c;这样的一组数据形成一棵树。即用于描述具有层次关系&#xff0c…

【Java|Stream流】不可变集合

文章目录 1.什么是不可变集合2.创建不可变集合的方式2.1 List类型2.2 Set2.3 Map 1.什么是不可变集合 不可变集合:不可以被修改的集合 不可变集合优点: 安全性 由于不可变集合不能被修改&#xff0c;所以可以安全地在多个线程之间共享&#xff0c;而不用担心被意外修改&#xf…

Burp Suite Professional 2024.8 for macOS x64 ARM64 - 领先的 Web 渗透测试软件

Burp Suite Professional 2024.8 for macOS x64 & ARM64 - 领先的 Web 渗透测试软件 世界排名第一的 Web 渗透测试工具包 请访问原文链接&#xff1a;https://sysin.org/blog/burp-suite-pro-mac/&#xff0c;查看最新版。原创作品&#xff0c;转载请保留出处。 作者主页…

大量图片进行自适应处理

首先我们手里有一个视频&#xff0c;但是我们对视频进行图像处理的话视频很难暂停进行观察&#xff0c;我们可以写一个按键坚挺&#xff0c;但是小编这里介绍另一个办法&#xff0c;大致思路为&#xff0c;把视频进行截帧为图片的集合&#xff0c;再对该集合进行统一的图像处理…

SQLite Insert 语句:一场数据“移民”大作战

SQLite Insert 语句&#xff1a;一场数据“移民”大作战 嘿&#xff0c;各位数据库的“移民官”们&#xff01;今天咱们来聊聊SQLite里的一场有趣活动——Insert语句&#xff0c;也就是数据“移民”。没错&#xff0c;就像国家与国家之间有移民&#xff0c;我们的数据也可以在…