• Java实现 LeetCode 380 常数时间插入、删除和获取随机元素


    380. 常数时间插入、删除和获取随机元素

    设计一个支持在平均 时间复杂度 O(1) 下,执行以下操作的数据结构。

    insert(val):当元素 val 不存在时,向集合中插入该项。
    remove(val):元素 val 存在时,从集合中移除该项。
    getRandom:随机返回现有集合中的一项。每个元素应该有相同的概率被返回。
    

    示例 :

    // 初始化一个空的集合。
    RandomizedSet randomSet = new RandomizedSet();
    
    // 向集合中插入 1 。返回 true 表示 1 被成功地插入。
    randomSet.insert(1);
    
    // 返回 false ,表示集合中不存在 2 。
    randomSet.remove(2);
    
    // 向集合中插入 2 。返回 true 。集合现在包含 [1,2] 。
    randomSet.insert(2);
    
    // getRandom 应随机返回 1 或 2 。
    randomSet.getRandom();
    
    // 从集合中移除 1 ,返回 true 。集合现在包含 [2] 。
    randomSet.remove(1);
    
    // 2 已在集合中,所以返回 false 。
    randomSet.insert(2);
    
    // 由于 2 是集合中唯一的数字,getRandom 总是返回 2 。
    randomSet.getRandom();
    
    class RandomizedSet {
    
        /**
            核心思路是用map记录数组每个值对应的下标,在插入和删除时不需要遍历数组寻找对应的数字下标,从而实现插入O(1)
            由于删除数组中的数据不是O(1),这里将数组中待删除的数字用数组最后一个数字代替,然后size - 1,下次insert的时候将size位置的数字覆盖,从而实现O(1)删除
        **/
        private Map<Integer,Integer> map;
        private List<Integer> list;
        private int size;
        /** Initialize your data structure here. */
        public RandomizedSet() {
            map = new HashMap<>();
            list = new ArrayList<>();
            size = 0;
        }
        
        /** Inserts a value to the set. Returns true if the set did not already contain the specified element. */
        public boolean insert(int val) {
            if(map.containsKey(val)){
                return false;
            }else{
                //这里需要用add(index,value)的重载方法,覆盖当前的size位置,这个方法在size == index也可以使用
                //不能使用set(index,value)方法,这个方法会在size == index时直接抛异常
                list.add(size,val);
                map.put(val,size);
                size++;
            }
            return true;
        }
        
        /** Removes a value from the set. Returns true if the set contained the specified element. */
        public boolean remove(int val) {
            if(!map.containsKey(val)){
                return false;
            }else{
                int oldIndex = map.get(val);
                int lastVal = list.get(size - 1);
                list.set(oldIndex,lastVal);
                map.put(lastVal,oldIndex);
                map.remove(val);
                size--;
            }
            return true;
        }
        
        /** Get a random element from the set. */
        public int getRandom() {
            Random ran = new Random();
            return list.get(ran.nextInt(size));
        }
    }
    
    /**
     * Your RandomizedSet object will be instantiated and called as such:
     * RandomizedSet obj = new RandomizedSet();
     * boolean param_1 = obj.insert(val);
     * boolean param_2 = obj.remove(val);
     * int param_3 = obj.getRandom();
     */
    
  • 相关阅读:
    win7,win10获取屏幕缩放适应截图
    cg语言学习&&阳春白雪GPU编程入门学习
    Unity Plugins的使用方法
    记录Unity的优化tip(不断更新)
    深入理解法线贴图原理
    读香菱学诗
    排序算法学习
    图的算法复习大纲
    Gama Space 和 Linear Space 学习
    BM算法学习
  • 原文地址:https://www.cnblogs.com/a1439775520/p/13075148.html
Copyright © 2020-2023  润新知