首页 > 其他分享 >leetcode面试经典150题- 380. O(1) 时间插入、删除和获取随机元素

leetcode面试经典150题- 380. O(1) 时间插入、删除和获取随机元素

时间:2024-08-14 18:07:40浏览次数:11  
标签:150 arr val int RandomizedSet isHave 380 total leetcode

 

https://leetcode.cn/problems/insert-delete-getrandom-o1/description/?envType=study-plan-v2&envId=top-interview-150

go

type RandomizedSet struct {
    isHave map[int]int
    total  int
    arr    []int
}

func Constructor() RandomizedSet {
    return RandomizedSet{
        isHave: make(map[int]int),
    }
}

func (this *RandomizedSet) Insert(val int) bool {
    if _, ok := this.isHave[val]; ok {
        return false
    }
    this.isHave[val] = this.total
    if len(this.arr) == this.total {
        this.arr = append(this.arr, 0)
    }
    this.arr[this.total] = val
    this.total++
    return true
}

func (this *RandomizedSet) Remove(val int) bool {
    if _, ok := this.isHave[val]; ok {
        // 把map对应下标换成交换后的
        this.isHave[this.arr[this.total-1]] = this.isHave[val]
        // 交换arr坐标里面的值
        this.arr[this.isHave[val]], this.arr[this.total-1] = this.arr[this.total-1], this.arr[this.isHave[val]]
        this.total--
        delete(this.isHave, val)
        return true
    }
    return false
}

func (this *RandomizedSet) GetRandom() int {
    return this.arr[rand.Intn(this.total)]
}

/**
 * Your RandomizedSet object will be instantiated and called as such:
 * obj := Constructor();
 * param_1 := obj.Insert(val);
 * param_2 := obj.Remove(val);
 * param_3 := obj.GetRandom();
 */

 

标签:150,arr,val,int,RandomizedSet,isHave,380,total,leetcode
From: https://www.cnblogs.com/MoonBeautiful/p/18359478

相关文章

  • leetcode面试经典150题- 45. 跳跃游戏 II
    https://leetcode.cn/problems/jump-game-ii/description/?envType=study-plan-v2&envId=top-interview-150gopackageleetcode150import"testing"funcTestJump2(t*testing.T){nums:=[]int{5,9,3,2,1,0,2,3,3,1,0,0}res:=j......
  • Leetcode 234.回文链表
    给你一个单链表的头节点 head ,请你判断该链表是否为回文链表。如果是,返回 true ;否则,返回 false 。示例1:输入:head=[1,2,2,1]输出:true示例2:输入:head=[1,2]输出:false提示:链表中节点数目在范围[1,105] 内0<=Node.val<=9进阶:你能否用 O(n) 时间复杂......
  • 每日一题:Leetcode-662 二叉树最大宽度
    力扣题目解题思路java代码力扣题目:给你一棵二叉树的根节点 root ,返回树的 最大宽度 。树的 最大宽度 是所有层中最大的 宽度 。每一层的 宽度 被定义为该层最左和最右的非空节点(即,两个端点)之间的长度。将这个二叉树视作与满二叉树结构相同,两端点间会出现一些......
  • 【知识】并查集的单点删除 &【题解】SP5150
    \(\mathfrak{1st.}\)前言-->题目传送门<--原先这道题的难度是紫,我觉得题目翻译看不懂,就去讨论版发了个贴,然后题管更新了题目翻译,并且把难度降到了蓝……\(\mathfrak{2nd.}\)思路赶时间或嫌啰嗦的可以直接跳到『思路归纳』。我们先从普通并查集开始思考对于删除单点\(x\),......
  • 高危漏洞CVE-2024-38077的修复指南
    “根据2024年8月9日,国家信息安全漏洞共享平台(CNVD)收录了Windows远程桌面许可服务远程代码执行漏洞(CNVD-2024-34918,对应CVE-2024-38077)。未经身份认证的攻击者可利用漏洞远程执行代码,获取服务器控制权限。目前,该漏洞的部分技术原理和概念验证伪代码已公开,厂商已发布安......
  • LeetCode 1383. Maximum Performance of a Team
    原题链接在这里:https://leetcode.com/problems/maximum-performance-of-a-team/description/题目:Youaregiventwointegers n and k andtwointegerarrays speed and efficiency bothoflength n.Thereare n engineersnumberedfrom 1 to n. speed[i] a......
  • Leetcode JAVA刷刷站(20)有效的括号
    一、题目概述二、思路方向     在Java中,要判断一个仅包含括号('(',')','{','}','[',']')的字符串是否有效,你可以使用栈(Stack)数据结构来实现。栈是一种后进先出(LIFO,LastInFirstOut)的数据结构,非常适合用来处理这类问题。以下是具体的实现步骤和代码示例:创......
  • LeetCode 1359. Count All Valid Pickup and Delivery Options
    原题链接在这里:https://leetcode.com/problems/count-all-valid-pickup-and-delivery-options/description/题目:Given n orders,eachorderconsistsofapickupandadeliveryservice.Countallvalidpickup/deliverypossiblesequencessuchthatdelivery(i)isalw......
  • leetcode面试经典150题-122. 买卖股票的最佳时机 II
    https://leetcode.cn/problems/best-time-to-buy-and-sell-stock-ii/description/?envType=study-plan-v2&envId=top-interview-150 gopackageleetcode150import"testing"funcTestMaxProfit2(t*testing.T){prices:=[]int{7,1,5,3,6,4}......
  • leetcode面试经典150题- 121. 买卖股票的最佳时机
    https://leetcode.cn/problems/best-time-to-buy-and-sell-stock/description/?envType=study-plan-v2&envId=top-interview-150gopackageleetcode150import("testing")funcTestMaxProfit(t*testing.T){prices:=[]int{2,1,2,0,1}......