【NO.12】LeetCode经典150题-380. O(1) 时间插入、删除和获取随机元素

【NO.12】LeetCode经典150题-380. O(1) 时间插入、删除和获取随机元素

380. O(1) 时间插入、删除和获取随机元素

【中等】

实现RandomizedSet 类:

  • RandomizedSet() 初始化 RandomizedSet 对象
  • bool insert(int val) 当元素 val 不存在时,向集合中插入该项,并返回 true ;否则,返回 false
  • bool remove(int val) 当元素 val 存在时,从集合中移除该项,并返回 true ;否则,返回 false
  • int getRandom() 随机返回现有集合中的一项(测试用例保证调用此方法时集合中至少存在一个元素)。每个元素应该有 相同的概率 被返回。

你必须实现类的所有函数,并满足每个函数的 平均 时间复杂度为 O(1)

示例 :

输入:
[“RandomizedSet”, “insert”, “remove”, “insert”, “getRandom”, “remove”, “insert”, “getRandom”]
[[], [1], [2], [2], [], [1], [2], []]
输出:
[null, true, false, true, 2, true, false, 2]

解释
RandomizedSet randomizedSet = new RandomizedSet();
randomizedSet.insert(1); // 向集合中插入 1 。返回 true 表示 1 被成功地插入。
randomizedSet.remove(2); // 返回 false ,表示集合中不存在 2 。
randomizedSet.insert(2); // 向集合中插入 2 。返回 true 。集合现在包含 [1,2] 。
randomizedSet.getRandom(); // getRandom 应随机返回 1 或 2 。
randomizedSet.remove(1); // 从集合中移除 1 ,返回 true 。集合现在包含 [2] 。
randomizedSet.insert(2); // 2 已在集合中,所以返回 false 。
randomizedSet.getRandom(); // 由于 2 是集合中唯一的数字,getRandom 总是返回 2 。

提示:

  • -231 <= val <= 231 - 1
  • 最多调用 insertremovegetRandom 函数 2 * 10^5
  • 在调用 getRandom 方法时,数据结构中 至少存在一个 元素。

解题

要求实现函数的平均时间复杂度都是O(1),那么:

  • 获取需要列表,用下标来操作,可以达到要求
  • 插入,列表的尾部插入,平均时间复杂度是O(1)
  • 删除,可以用哈希表来保存元素对应的下标,然后找到下标后,操作列表
class RandomizedSet {

    // 数组,用于获取元素
    List<Integer> nums;
    // map.key为数,value为下标,用于确定下标位置
    Map<Integer, Integer> map;
    Random random;

    public RandomizedSet() {
        nums = new ArrayList<Integer>();
        map = new HashMap<Integer, Integer>();
        random = new Random();
    }
    
    // insert时,数组列表与map都要维护
    public boolean insert(int val) {
        if (!map.containsKey(val)) {
            int length = nums.size();
            nums.add(val);
            map.put(val, length);
            return true;
        }
        return false;
    }
    
    // 先找下标
    // 用列表最后一个数覆盖要删除的数,并更新map
    // 删除最后一个数
    public boolean remove(int val) {
        if (map.containsKey(val)) {
            int length = nums.size();
            Integer lastNum = nums.get(length - 1);
            int targetIndex = map.get(val);
            // 用最后一个数覆盖要删除的数
            nums.set(targetIndex, lastNum);
            map.put(lastNum, targetIndex);
            // 删除最后一个数
            nums.remove(length - 1);
            map.remove(val);
            return true;
        }
        return false;
    }
    
    public int getRandom() {
        int index = random.nextInt(nums.size());
        return nums.get(index);
    }
}

/**
 * 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();
 */
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

悬浮海

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值