zoukankan      html  css  js  c++  java
  • 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()
     */
  • 相关阅读:
    题目:返回一个整数数组中最大子数组的和。(要求程序必须能处理1000 个元素)
    四则运算三(接受用户输入答案,并判断对错。)
    二维数组
    结对开发(一位数组)
    测试四则运算
    四则运算2
    程序设计思路
    项目计划总结
    小学二年级题目的改进
    二年级题目的改进
  • 原文地址:https://www.cnblogs.com/johnnyzhao/p/13286402.html
Copyright © 2011-2022 走看看