381. O(1) 时间插入、删除和获取随机元素 - 允许重复

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

注意: 允许出现重复元素。

insert(val):向集合中插入元素 val。
remove(val):当 val 存在时,从集合中移除一个 val。
getRandom:从现有集合中随机获取一个元素。每个元素被返回的概率应该与其在集合中的数量呈线性相关。
示例:

// 初始化一个空的集合。
RandomizedCollection collection = new RandomizedCollection();

// 向集合中插入 1 。
返回 true 表示集合不包含 1 。
collection.insert(1);

// 向集合中插入另一个 1 。
返回 false 表示集合包含 1 。
集合现在包含 [1,1] 。
collection.insert(1);

// 向集合中插入 2 ,返回 true 。
集合现在包含 [1,1,2] 。
collection.insert(2);

// getRandom 应当有 2/3 的概率返回 1 ,1/3 的概率返回 2 。
collection.getRandom();

// 从集合中删除 1 ,返回 true 。
集合现在包含 [1,2] 。
collection.remove(1);

// getRandom 应有相同概率返回 1 和 2 。
collection.getRandom();

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/insert-delete-getrandom-o1-duplicates-allowed
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

用数组可以实现O(1)的插入、取随机,问题就剩下了删除。用HashMap存HashSet可以O(1)的时间访问到一个数,但是如果直接原地删除需要把右侧的数往左移一位那怎么办呢?
把数组最后一个数移过来然后数组长度-1就好了…要说关键就这一句话
实现用了ArrayList,不然就得自己实现vector了:

class RandomizedCollection {
    //有点扯的题
    HashMap<Integer, HashSet<Integer>> index;
    ArrayList<Integer> nums;
    Random r;

    /** Initialize your data structure here. */
    public RandomizedCollection() {
        nums = new ArrayList<>();
        index = new HashMap<>();
        r = new Random();
    }
    
    /** Inserts a value to the collection. Returns true if the collection did not already contain the specified element. */
    public boolean insert(int val) {
        int ii = nums.size();
        nums.add(val);
        HashSet<Integer> set = index.computeIfAbsent(val, (k) -> new HashSet<Integer>());
        set.add(ii);
        return set.size() == 1;
    }
    
    /** Removes a value from the collection. Returns true if the collection contained the specified element. */
    public boolean remove(int val) {
        //如果直接remove会导致错位 所以和最后一个交换位置再remove 这个还是挺巧妙的 不过用ArrayList真的好吗
        if(!index.containsKey(val)) {
            return false;
        }
        var set = index.get(val);
        if(set.size() == 1) {
            index.remove(val);
        }
        int ii = set.iterator().next();
        set.remove(ii);
        if(ii != nums.size() - 1) {
            val = nums.get(nums.size() - 1);
            nums.set(ii, val);
            set = index.get(val);
            set.remove(nums.size() - 1);
            set.add(ii);
        }
        
        nums.remove(nums.size() - 1);
        return true;
    }
    
    /** Get a random element from the collection. */
    public int getRandom() {
        return nums.get(r.nextInt(nums.size()));
    }
}

/**
 * Your RandomizedCollection object will be instantiated and called as such:
 * RandomizedCollection obj = new RandomizedCollection();
 * boolean param_1 = obj.insert(val);
 * boolean param_2 = obj.remove(val);
 * int param_3 = obj.getRandom();
 */
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值