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日

相关推荐

  • 程序员面试金典01.07-旋转矩阵

    原题(来源Leetcode) 给你一幅由 N × N 矩阵表示的图像,其中每个像素的大小为 4 字节。请你设计一种算法,将图像旋转 90 度。 不占用额外内存空间能否做到? 示例 …

    算法 2020年4月7日
    0410
  • leetcode1103-分糖果II

    原题 排排坐,分糖果。 我们买了一些糖果 candies,打算把它们分给排好队的 n = num_people 个小朋友。 给第一个小朋友 1 颗糖果,第二个小朋友 2 颗,依此类…

    算法 2020年3月5日
    0140
  • leetcode40-组合总和II

    原题 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合。 candidates 中的每个数字…

    算法 2020年5月2日
    0730
  • leetcode383-赎金信

    原题 https://leetcode.cn/problems/ransom-note/ 解法 记录下字母出现次数 func canConstruct(ransomNote str…

    算法 2024年3月26日
    030
  • leetcode71-简化路径

    原题 以 Unix 风格给出一个文件的绝对路径,你需要简化它。或者换句话说,将其转换为规范路径。 在 Unix 风格的文件系统中,一个点(.)表示当前目录本身;此外,两个点 (..…

    算法 2020年1月23日
    090
  • leetcode38-外观数列

    原题 「外观数列」是一个整数序列,从数字 1 开始,序列中的每一项都是对前一项的描述。前五项如下: 1. 1 2. 11 3. 21 4. 1211 5. 111221 1 被读作…

    算法 2020年4月30日
    0290
  • leetcode199-二叉树的右视图

    原题 给定一棵二叉树,想象自己站在它的右侧,按照从顶部到底部的顺序,返回从右侧所能看到的节点值。 示例: 输入: [1,2,3,null,5,null,4] 输出:&nb…

    算法 2020年4月22日
    0110
  • leetcode994-腐烂的橘子

    原题 在给定的网格中,每个单元格可以有以下三个值之一: 值 0 代表空单元格;值 1 代表新鲜橘子;值 2 代表腐烂的橘子。每分钟,任何与腐烂的橘子(在 4 个正方向上)相邻的新鲜…

    2020年3月4日
    0100
  • leetcode144-二叉树的前序遍历

    原题 给定一个二叉树,返回它的 前序 遍历。 示例: 输入: [1,null,2,3]  1   \    2 &nbs…

    算法 2020年1月10日
    0130
  • leetcode235-二叉搜索树的最近公共祖先

    原题 给定一个二叉二叉树, 找到该树中两个指定节点的最近公共祖先。 百度百科中最近公共祖先的定义为:“对于有根树 T 的两个结点 p、q,最近公共祖先表示为一个结点 x,满足 x …

    2020年1月17日
    0100

发表回复

登录后才能评论