剑指Offer专项突破版(30)—— 插入、删除和随机访问都是 O(1) 的容器

题目

剑指 Offer II 030. 插入、删除和随机访问都是 O(1) 的容器

在这里插入图片描述

思路

如果只用哈希表,则不能等概率地返回其中的每个数值

想要等概率的返回每个元素,就需要将每个数对应到一个范围从0~size-1的索引

getRandom首先从0~size-1中随机选一个数,再反查是哪个数字

// 数字到索引
private Map<Integer,Integer> num2Index = new HashMap<>();
// 索引到数字
private Map<Integer,Integer> index2Num = new HashMap<>();

insert时,需要将新元素和size绑定,然后size++

public boolean insert(int val) {
    if (num2Index.containsKey(val)) {
        return false;
    }

    num2Index.put(val,size);
    index2Num.put(size,val);
    size++;
    return true;
}

remove时,如果只是简单地将数据从num2Indexindex2Num中删除,会造成索引空洞

因此如果删除的不是最后一个数,需要将最后一个数填到被删除的数位置上,即将被删除数的索引赋给最后一个数,然后size--

public boolean remove(int val) {
    if (!num2Index.containsKey(val)) {
        return false;
    }
    // 被删除数的索引
    int removeIndex = num2Index.get(val);
    num2Index.remove(val);
    if (removeIndex != size-1) {
        // 将removeIndex赋给最后一个数
        num2Index.put(index2Num.get(size-1),removeIndex);
        index2Num.put(removeIndex,index2Num.get(size-1));
    }
    index2Num.remove(size-1);
    size--;
    return true;
}

getRandom就比较简单了:先生成0~size-1随机数,再根据随机数反查元素

public int getRandom() {
    return index2Num.get(new Random().nextInt(size));
}

代码


class RandomizedSet {
    private Map<Integer,Integer> num2Index = new HashMap<>();
    private Map<Integer,Integer> index2Num = new HashMap<>();
    private int size;

    /** Initialize your data structure here. */
    public RandomizedSet() {
        

    }

    /** Inserts a value to the set. Returns true if the set did not already contain the specified element. */
    public boolean insert(int val) {
        if (num2Index.containsKey(val)) {
            return false;
        }

        num2Index.put(val,size);
        index2Num.put(size,val);
        size++;
        return true;
        
    }

    /** Removes a value from the set. Returns true if the set contained the specified element. */
    public boolean remove(int val) {
        if (!num2Index.containsKey(val)) {
            return false;
        }
        int removeIndex = num2Index.get(val);
        num2Index.remove(val);
        if (removeIndex != size-1) {
            num2Index.put(index2Num.get(size-1),removeIndex);
            index2Num.put(removeIndex,index2Num.get(size-1));
        }
        index2Num.remove(size-1);
        size--;
        return true;
    }

    /** Get a random element from the set. */
    public int getRandom() {
        return index2Num.get(new Random().nextInt(size));
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值