leetcode 380. Insert Delete GetRandom O(1) 、381. Insert Delete GetRandom O(1)

380. Insert Delete GetRandom O(1)

实现插入、删除、获得随机数功能,且时间复杂度都在O(1)。实际上在插入、删除两个功能中都包含了查找功能,当然查找也必须是O(1)。

数组可以实现插入、删除、获得随机数O(1),但查找就不行了。(当然对于数组,直接删除的时间复杂度不是O(1),因为可能需要移动)

hash、set都是红黑树实现的,查找、插入、删除的时间复杂度在O(logn)。

unordered_map、unordered_set是哈希表实现的,查找、插入、删除的时间复杂度在O(1)。但unordered_map、unordered_set都只能用迭代器访问,无法用索引访问,所以获得随机数的时间复杂度不是O(1)。但是unordered_map、unordered_set都还是有size()的函数,只是不像vector那样用size来获得index访问。

unordered_set用迭代器访问的方式如下:

#include <iostream>
#include <unordered_set>


using namespace std;

int main(){
    unordered_set<int> s;
    s.insert(10);
    s.insert(11);
    s.insert(12);
    for(auto it = s.begin();it != s.end();it++)
        cout << *it << endl;
}

 注意:result.back()返回的是最后一个位置的数值,不是下标

      在删除的函数中,除了删除vector中存储的数值,还要删除map中数值与索引,不然下次访问还会有这个被删除的数字

vector存储数,unordered_map存储数和对应在vector的下标

class RandomizedSet {
public:
    /** Initialize your data structure here. */
    RandomizedSet() {
        
    }
    
    /** Inserts a value to the set. Returns true if the set did not already contain the specified element. */
    bool insert(int val) {
        if(m.find(val) != m.end())
            return false;
        result.push_back(val);
        m[val] = result.size() - 1;
        return true;
    }
    
    /** Removes a value from the set. Returns true if the set contained the specified element. */
    bool remove(int val) {
        if(m.find(val) == m.end())
            return false;
        int index = m[val];
        result[index] = result.back();
        m[result.back()] = index;
        result.pop_back();
        m.erase(val);
        return true;
    }
    
    /** Get a random element from the set. */
    int getRandom() {
        int index = rand() % result.size();
        return result[index];
    }
private:
    vector<int> result;
    unordered_map<int,int> m;
};

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

与380题不同,这个题允许重复

unordered_map存储的数和数对应存储索引的集合,用set存储

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) {
        int index = result.size();
        result.push_back(val);
        m[val].insert(index);
        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 index = *m[val].begin();
        m[val].erase(index);
        if(index != result.size() - 1){
            result[index] = result.back();
            m[result.back()].erase(result.size() - 1);
            m[result.back()].insert(index);
        }
        result.pop_back();
        return true;
    }
    
    /** Get a random element from the collection. */
    int getRandom() {
        if(result.empty())
            return -1;
        int index = rand()%result.size();
        return result[index];
    }
private:
    vector<int> result;
    unordered_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/ymjyqsx/p/10882501.html