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

原题

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

  1. insert(val):当元素 val 不存在时,向集合中插入该项。
  2. remove(val):元素 val 存在时,从集合中移除该项。
  3. 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();

解法

思想

  1. 初始想法

哈希表的插入删除的时间复杂度都是O(1),获取的时候可以通过EntrySet。
所以这道题是不能用HashSet的。

这样虽然获取随机元素的时候时间复杂度最高可能是O(n),但仍比遍历一遍Set转ArrayList好很多。

  1. 正确解法

哈希表插入和删除都是O(1),而顺序表随机访问则是O(1),可以使用ArrayList来存储所有的数据。但是必须解决ArrayList删除元素的O(n)问题。

于是可以:

在哈希表中用value-index来记录值和在list中的下标的对应关系,如图所示

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

当删除元素时,size减一,用list中最后那个元素替换要删除的那个元素,并且将哈希表中的对应关系改过来(用要删除的元素的index替换list中最后那个元素对应的index):

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

此时若要随机访问元素,只需获取list中前3(size)个元素中的一个。

那么如果需要继续插入元素,只需从list中下标为3(size)处替换掉后面那个元素或是在后面那个元素之前插入(这里如果用插入是使用add(index,value)方法,个人觉得比起set会增加时间复杂度,因为使用add后面的元素都需要向后移动,虽然jdk源码中使用了System.arraycopy即内存拷贝来优化,但是也比直接替换的时间复杂度更高):

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

如果不是替换元素而是add操作,这里会变成1-4-2-8-4

代码

  1. 初始想法
class RandomizedSet {
    Object none = new Object();
    HashMap<Integer,Object> map;
    Random random;
    /** Initialize your data structure here. */
    public RandomizedSet() {
        map = new HashMap<>();
        random = new Random();
    }

    /** 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;
        map.put(val,none);
        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;
        map.remove(val);
        return true;
    }

    /** Get a random element from the set. */
    public int getRandom() {
        int ran = random.nextInt(map.size());  
        int n = 0;
        for(Map.Entry<Integer,Object> i:map.entrySet()){
            if(n==ran) return i.getKey();
            n++;
        }
        return 0;
    }
}

/**
 * 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();
 */
  1. 正确解法(来源:leetcode用户,添加元素时是直接插入)
class RandomizedSet {

    Map<Integer,Integer> map; // 存放值和在 list 的下标位置的映射
    List<Integer> list;       // 存放要插入数据的结构
    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{
            // 插入数据,并更新 map 的映射后将长度加一
            list.add(size,val);
            map.put(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(!map.containsKey(val)) return false;
        else if( size == 0 ){ map.remove(val);}
        else{
            // 取到 list 末尾的数据
            int tailKey = list.get(size-1);
            // 然后将要原先 map 中得 val-index 映射改为 tailKey-index
            map.put(tailKey,map.get(val));
            // 在 map 中取得 val 在 list 的位置,然后根据这个位置用末尾元素 tailKey 替代
            list.set(map.get(val),tailKey);
            // 在 map 中删除 val 的映射
            map.remove(val);
            size--;
        }
        return true;
    }

    /** Get a random element from the set. */
    public int getRandom() {
        Random rand = new Random();
         // rand.nextInt(size) 产生的是 0 到 size(不包括 size) 的数据
        return list.get(rand.nextInt(size));
    }
}
type RandomizedSet struct {
    Value2Index map[int]int
    List []int
}


func Constructor() RandomizedSet {
    return RandomizedSet{
        Value2Index: map[int]int{},
        List: []int{},
    }
}


func (this *RandomizedSet) Insert(val int) bool {
    _, ok := this.Value2Index[val] 
    if ok {
        return false
    }
    this.List = append(this.List, val)
    this.Value2Index[val] = len(this.List) - 1
    return true
}


func (this *RandomizedSet) Remove(val int) bool {
    index, ok := this.Value2Index[val] 
    if !ok {
        return false
    }
    delete(this.Value2Index, val)

    length := len(this.List)
    if length > 0{
        // 把最后一个元素填补到要删除的元素位置上
        lastPosValue := this.List[length - 1]
        this.List = this.List[0:length - 1]
        // 如果要删的元素本身就是最后一个元素,清空数组即可
        if index != length - 1{
            this.List[index] = lastPosValue
            this.Value2Index[lastPosValue] = index
        }
    } 
    return true
}


func (this *RandomizedSet) GetRandom() int {
    length := len(this.List)
    if length == 0{
        return 0
    }
    return this.List[rand.Intn(length)]
}

原创文章,作者:彭晨涛,如若转载,请注明出处:https://www.codetool.top/article/leetcode380-%e5%b8%b8%e6%95%b0%e6%97%b6%e9%97%b4%e6%8f%92%e5%85%a5%e3%80%81%e5%88%a0%e9%99%a4%e5%92%8c%e8%8e%b7%e5%8f%96%e9%9a%8f%e6%9c%ba%e5%85%83%e7%b4%a0/

(0)
彭晨涛彭晨涛管理者
上一篇 2019年12月28日
下一篇 2019年12月29日

相关推荐

  • leetcode599-两个列表的最小索引总和

    原题 假设Andy和Doris想在晚餐时选择一家餐厅,并且他们都有一个表示最喜爱餐厅的列表,每个餐厅的名字用字符串表示。 你需要帮助他们用最少的索引和找出他们共同喜爱的餐厅。 如果…

    算法 2019年12月22日
    0120
  • leetcode733-图像渲染

    原题 有一幅以二维整数数组表示的图画,每一个整数表示该图画的像素值大小,数值在 0 到 65535 之间。 给你一个坐标 (sr, sc) 表示图像渲染开始的像素值(行 ,列)和一…

    2019年12月13日
    0100
  • leetcode1014-最佳观光组合

    原题 给定正整数数组 A,A[i] 表示第 i 个观光景点的评分,并且两个景点 i 和 j 之间的距离为 j - i。 一对景点(i < j)组成的观光组合的得分为(A[i]…

    算法 2020年6月17日
    01950
  • leetcode12-整数转罗马数字

    原题 罗马数字包含以下七种字符: I, V, X, L,C,D 和 M。 字符 数值 I 1 V 5 X 10 L 50 C 100 D 500 M 1000 例如, 罗马数字 2…

    算法 2020年2月29日
    070
  • leetcode18-四数之和

    原题 给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 ta…

    算法 2020年5月5日
    0120
  • leetcode8-字符串转换整数 (atoi)

    原题 请你来实现一个 atoi 函数,使其能将字符串转换成整数。 首先,该函数会根据需要丢弃无用的开头空格字符,直到寻找到第一个非空格的字符为止。接下来的转化规则如下: 如果第一个…

    算法 2020年4月3日
    0270
  • leetcode31-下一个排列

    原题 整数数组的一个 排列 就是将其所有成员以序列或线性顺序排列。 例如,arr = [1,2,3] ,以下这些都可以视作 arr 的排列:[1,2,3]、[1,3,2]、[3,1…

    算法 2022年4月13日
    02583
  • leetcode373-查找和最小的K对数字

    原题 给定两个以升序排列的整形数组 nums1 和 nums2, 以及一个整数 k。 定义一对值 (u,v),其中第一个元素来自 nums1,第二个元素来自 nums2。 找到和最…

    算法 2020年2月13日
    0160
  • leetcode2-两数相加

    原题 给出两个 非空 的链表用来表示两个非负的整数。其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字。 如果,我们将这两个数相加起来,则会返回…

    算法 2019年12月17日
    080
  • leetcode376-摆动序列

    原题 如果连续数字之间的差严格地在正数和负数之间交替,则数字序列称为摆动序列。第一个差(如果存在的话)可能是正数或负数。少于两个元素的序列也是摆动序列。 例如, [1,7,4,9,…

    算法 2020年2月18日
    0110

发表回复

登录后才能评论