380. Insert Delete GetRandom O(1)

复制代码
package LeetCode_380


/**
 * 380. Insert Delete GetRandom O(1)
 * https://leetcode.com/problems/insert-delete-getrandom-o1/description/
 *
Design a data structure that supports all following operations in average O(1) time.
==insert(val): Inserts an item val to the set if not already present.
==remove(val): Removes an item val from the set if present.
==getRandom: Returns a random element from current set of elements (it's guaranteed that at least one element exists when this method is called).
Each element must have the same probability of being returned.
 * */
class RandomizedSet() {
    /*
    * solution: HashMap+Array; key in HashMap is num and value is the index in Array
    * insert O(1):HashMap
    * delete O(1):HashMap
    * get random O(1):List or Array, because it save item in memory continuously
    * */
    /** Initialize your data structure here. */
    val map = HashMap<Int, Int>()
    val list = ArrayList<Int>()
    //kotlin's random was not work in here??
    var random = java.util.Random()

    /** Inserts a value to the set. Returns true if the set did not already contain the specified element. */
    fun insert(`val`: Int): Boolean {
        if (map.containsKey(`val`)) {
            return false
        }
        //insert value into the tail of Array
        val index = list.size
        list.add(`val`)
       map.put(`val`, index)
        return true
    }

    /** Removes a value from the set. Returns true if the set contained the specified element. */
    fun remove(`val`: Int): Boolean {
        //remove key in map and
        if (!map.containsKey(`val`)) {
            return false
        }
        val indexInArray = map.get(`val`)!!
        map.put(list.elementAt(list.lastIndex), indexInArray)
        //replace val with the element in the last of array and remove it
        list.set(indexInArray, list.elementAt(list.lastIndex))
        //remove the element in the last of array
        list.removeAt(list.size - 1)
        //remove key in map
        map.remove(`val`)
        return true
    }

    /** Get a random element from the set. */
    fun getRandom(): Int {
        val randomIndex = random.nextInt(list.size)
        return list[randomIndex]
    }
}

/**
 * Your RandomizedSet object will be instantiated and called as such:
 * var obj = RandomizedSet()
 * var param_1 = obj.insert(`val`)
 * var param_2 = obj.remove(`val`)
 * var param_3 = obj.getRandom()
 */
复制代码

 related:

381. Insert Delete GetRandom O(1) - Duplicates allowed

posted @   johnny_zhao  阅读(148)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)
点击右上角即可分享
微信分享提示