• 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()
     */
  • 相关阅读:
    单例
    Label自适应高度
    通知中心(以夜间模式为例)
    ios VFL屏幕自适应
    网络请求数据(同步POST,异步POST)
    linux 设备文件
    linux 文件存取 软硬联接的区别
    linux 磁盘管理与文件系统
    linux开机过程
    Build Antlr4 projects with eclipse java project template.
  • 原文地址:https://www.cnblogs.com/johnnyzhao/p/13286402.html
Copyright © 2020-2023  润新知