leetcode 381.Insert Delete GetRandom

这道题中要求使用O(1)的方法来删除和插入元素的,那么首先需要寻找到对应的元素,这个可以使用map的O(1)的查询时间的,然后是删除对应的元素的,那么可以根据 堆排序中类似的做法把最后面的元素插入到前面来并且置换掉对应的值的。

class RandomizedCollection {
public:
    /** Initialize your data structure here. */
    RandomizedCollection() {

    }

    /** Inserts a value to the collection. Returns true if the collection did not already contain the specified element. */
    bool insert(int val) {
        m[val].insert(num.size());
        num.push_back(val);
        return m[val].size()==1;
    }

    /** Removes a value from the collection. Returns true if the collection contained the specified element. */
    bool remove(int val) {
        if(m[val].empty()) return false;
        int pos=*m[val].begin();
        m[val].erase(pos);
        if(pos!=num.size()-1){
            int temp=num.back();
            num[pos]=temp;
            m[temp].erase(num.size()-1);
            m[temp].insert(pos);
        }
        num.pop_back();
        return true;
    }

    /** Get a random element from the collection. */
    int getRandom() {
        return num[rand()%num.size()];
    }
private:
    vector<int> num;
    map<int, set<int>> m;
};

/**
 * Your RandomizedCollection object will be instantiated and called as such:
 * RandomizedCollection obj = new RandomizedCollection();
 * bool param_1 = obj.insert(val);
 * bool param_2 = obj.remove(val);
 * int param_3 = obj.getRandom();
 */

原文地址:https://www.cnblogs.com/newnoobbird/p/9636116.html

时间: 2024-10-25 17:24:29

leetcode 381.Insert Delete GetRandom的相关文章

LeetCode 381. Insert Delete GetRandom O(1) - Duplicates allowed (插入删除和获得随机数 常数时间 允许重复项)

Design a data structure that supports all following operations in average O(1) time. Note: Duplicate elements are allowed. insert(val): Inserts an item val to the collection. remove(val): Removes an item val from the collection if present. getRandom:

381.&#160;Insert Delete GetRandom O(1) - Duplicates allowed

381. Insert Delete GetRandom O(1) - Duplicates allowed https://www.youtube.com/watch?v=mRTgft9sBhA 这个自己写写,花花讲的思路还行 https://zxi.mytechroad.com/blog/hashtable/leetcode-381-insert-delete-getrandom-o1-duplicates-allowed/ 没通过 class RandomizedCollection {

LeetCode 380. Insert Delete GetRandom O(1)

380. Insert Delete GetRandom O(1) Add to List Description Submission Solutions Total Accepted: 21771 Total Submissions: 56175 Difficulty: Medium Contributors: Admin Design a data structure that supports all following operations in average O(1) time.

381. Insert Delete GetRandom O(1) - Duplicates allowed - Hard

Design a data structure that supports all following operations in average O(1) time. Note: Duplicate elements are allowed. insert(val): Inserts an item val to the collection. remove(val): Removes an item val from the collection if present. getRandom:

【Leetcode】Insert Delete GetRandom O(1) - Duplicates allowed

题目链接:https://leetcode.com/problems/insert-delete-getrandom-o1-duplicates-allowed/ 题目: Design a data structure that supports all following operations in average O(1) time. Note: Duplicate elements are allowed. insert(val): Inserts an item val to the c

【Leetcode】Insert Delete GetRandom O(1)

题目链接:https://leetcode.com/problems/insert-delete-getrandom-o1/ 题目: Design a data structure that supports all following operations in average O(1) time. insert(val): Inserts an item val to the set if not already present. remove(val): Removes an item v

LeetCode 380. Insert Delete GetRandom O(1) (插入删除和获得随机数 常数时间)

Design a data structure that supports all following operations in average O(1) time. insert(val): Inserts an item val to the set if not already present. remove(val): Removes an item val from the set if present. getRandom: Returns a random element fro

[leetcode]380. Insert Delete GetRandom O(1)设计数据结构,实现存,删,随机取的时间复杂度为O(1)

题目: Design a data structure that supports all following operations in average O(1) time. 1.insert(val): Inserts an item val to the set if not already present.2.remove(val): Removes an item val from the set if present.3.getRandom: Returns a random ele

[leetcode]380. Insert Delete GetRandom O(1)常数时间插入删除取随机值

Design a data structure that supports all following operations in average O(1) time. insert(val): Inserts an item val to the set if not already present. remove(val): Removes an item val from the set if present. getRandom: Returns a random element fro