1. 题目要求
设计一种结构,在该结构中有如下三个功能:
- insert(key):将某个key加入到该结构,做到不重复加入。
- delete(key):将原本在结构中的某个key移除。
- getRandom(): 等概率随机返回结构中的任何一个key。
【要求】 Insert、delete和getRandom方法的时间复杂度都是 O ( 1 ) O(1) O(1)
2. 思路
用两个hash表
为了得到key -> index以及index -> key的对应关系,使用两个哈希表来建立两者之间的映射。
- map1中存放的 key value 分别是 相应的值 和对应的插入顺序
- map2中正好相反, key value 分别是对应的插入顺序和相应的值
具体操作:
- 针对insert我们可以实现二个map同步操作,一个插入(key, index),另外一个插入(index, key),然后使用size计数即可,保持同步。
- 针对getRandom,虽然Hash表返回的是近似等概论的,但是不是严格等概率的,所有我们利用随机数从(index, key)中得到一个key。
- 针对delete操作,我们确实可以直接在(key, index)进行操作,但是这样我们在使用getRandom函数之后它会产生空洞了,所以一种思路就是我们可以借助最后一行(key,index)进行赋值给需要删除的key,这样就可以消除空洞。
3. 准备
为了实现C++版本的代码,我们需要对一些常见的
map
,hash_map
,unordered_map
常见的函数使用熟悉哈。可以参考下面的几篇博客作为先导。
- map 学习(上)——C++中 map 的使用
- map 学习(下)——C++ 中的 hash_map, unordered_map
- c++ unordered_map 判断某个键是否存在
- C++11 中的 emplace
- cppreference.com
- 十一、从头到尾解析Hash表算法
- 哈希表(散列表)原理详解
好了上面对hash
有了基本的了解,我们就可以愉快的写代码了。
4. 代码
#include <iostream>
#include <unordered_map>
#include <cstdlib>
class RandomPool {
public:
std::unordered_map<std::string, int> keyIndexMap;
std::unordered_map<int, std::string> indexKeyMap;
int size;
RandomPool(): size(0) {} // default constructor
void insertKey(std::string key) {
if (keyIndexMap.find(key) == keyIndexMap.end()) { // if don't have key
keyIndexMap.emplace(key, size); // we can also insert({key, size}) instead
indexKeyMap.emplace(size, key);
size++;
}
}
void deleteKey(std::string key) {
if (keyIndexMap.find(key) != keyIndexMap.end()) { // if we have key
int deleteIndex = keyIndexMap.at(key); // find we we want to delete the index
int lastIndex = --size; // last index
std::string lastKey = indexKeyMap.at(lastIndex); // find the last key
keyIndexMap.erase(key);
keyIndexMap.erase(lastKey);
indexKeyMap.erase(deleteIndex);
indexKeyMap.erase(lastIndex);
keyIndexMap.emplace(lastKey, deleteIndex);
indexKeyMap.emplace(deleteIndex, lastKey);
}
}
std::string getRandomKey() {
int random = rand() % size; // get [0, size-1]
return indexKeyMap.at(random); // we can also use indexKeyMap[random] instead
}
};
int main()
{
RandomPool randomPool;
randomPool.insertKey("A");
randomPool.insertKey("B");
randomPool.insertKey("C");
std::cout << "===================Insert key===================" << std::endl;
std::cout << "keyIndexMap: " << std::endl;
for (auto& it: randomPool.keyIndexMap) {
std::cout << it.first << ": " << it.second << std::endl;
}
std::cout << "indexKeyMap: " << std::endl;
for (auto it = randomPool.indexKeyMap.begin(); it != randomPool.indexKeyMap.end(); ++it) {
std::cout << it->first << ": " << it->second << std::endl;
}
std::cout << "===================Random key===================" << std::endl;
std::string randomKey1 = randomPool.getRandomKey();
std::string randomKey2 = randomPool.getRandomKey();
std::string randomKey3 = randomPool.getRandomKey();
std::cout << "key1: " << randomKey1 << "\n" << "key2: "
<< randomKey2 << "\n" << "key3: " << randomKey3 << std::endl;
std::cout << "===================Delete key===================" << std::endl;
randomPool.deleteKey("A"); // delete "A"
std::cout << "keyIndexMap: " << std::endl;
for (auto& it: randomPool.keyIndexMap) {
std::cout << it.first << ": " << it.second << std::endl;
}
std::cout << "indexKeyMap: " << std::endl;
for (auto it = randomPool.indexKeyMap.begin(); it != randomPool.indexKeyMap.end(); ++it) {
std::cout << it->first << ": " << it->second << std::endl;
}
std::cout << "==================After delete random key========" << std::endl;
std::string randomKey11 = randomPool.getRandomKey();
std::string randomKey22 = randomPool.getRandomKey();
std::string randomKey33 = randomPool.getRandomKey();
std::cout << "key1: " << randomKey11 << "\n" << "key2: "
<< randomKey22 << "\n" << "key3: " << randomKey33 << std::endl;
return 0;
}
进一步的,我们为了不使用std::string
作为我们的key
,我们可以使用类模板。
#include <iostream>
#include <unordered_map>
#include <cstdlib>
template <typename T> class RandomPool {
public:
std::unordered_map<T, int> keyIndexMap;
std::unordered_map<int, T> indexKeyMap;
int size;
RandomPool(): size(0) {} // default constructor
void insertKey(T key) {
if (keyIndexMap.find(key) == keyIndexMap.end()) { // if don't have key
keyIndexMap.emplace(key, size); // we can also insert({key, size}) instead
indexKeyMap.emplace(size, key);
size++;
}
}
void deleteKey(T key) {
if (keyIndexMap.find(key) != keyIndexMap.end()) { // if we have key
int deleteIndex = keyIndexMap.at(key); // find we we want to delete the index
int lastIndex = --size; // last index
T lastKey = indexKeyMap.at(lastIndex); // find the last key
keyIndexMap.erase(key);
keyIndexMap.erase(lastKey);
indexKeyMap.erase(deleteIndex);
indexKeyMap.erase(lastIndex);
keyIndexMap.emplace(lastKey, deleteIndex);
indexKeyMap.emplace(deleteIndex, lastKey);
}
}
T getRandomKey() {
int random = rand() % size; // get [0, size-1]
return indexKeyMap.at(random); // we can also use indexKeyMap[random] instead
}
};
int main()
{
RandomPool<std::string> randomPool; // template
randomPool.insertKey("A");
randomPool.insertKey("B");
randomPool.insertKey("C");
std::cout << "===================Insert key===================" << std::endl;
std::cout << "keyIndexMap: " << std::endl;
for (auto& it: randomPool.keyIndexMap) {
std::cout << it.first << ": " << it.second << std::endl;
}
std::cout << "indexKeyMap: " << std::endl;
for (auto it = randomPool.indexKeyMap.begin(); it != randomPool.indexKeyMap.end(); ++it) {
std::cout << it->first << ": " << it->second << std::endl;
}
std::cout << "===================Random key===================" << std::endl;
std::string randomKey1 = randomPool.getRandomKey();
std::string randomKey2 = randomPool.getRandomKey();
std::string randomKey3 = randomPool.getRandomKey();
std::cout << "key1: " << randomKey1 << "\n" << "key2: "
<< randomKey2 << "\n" << "key3: " << randomKey3 << std::endl;
std::cout << "===================Delete key===================" << std::endl;
randomPool.deleteKey("A"); // delete "A"
std::cout << "keyIndexMap: " << std::endl;
for (auto& it: randomPool.keyIndexMap) {
std::cout << it.first << ": " << it.second << std::endl;
}
std::cout << "indexKeyMap: " << std::endl;
for (auto it = randomPool.indexKeyMap.begin(); it != randomPool.indexKeyMap.end(); ++it) {
std::cout << it->first << ": " << it->second << std::endl;
}
std::cout << "==================After delete random key========" << std::endl;
std::string randomKey11 = randomPool.getRandomKey();
std::string randomKey22 = randomPool.getRandomKey();
std::string randomKey33 = randomPool.getRandomKey();
std::cout << "key1: " << randomKey11 << "\n" << "key2: "
<< randomKey22 << "\n" << "key3: " << randomKey33 << std::endl;
return 0;
}
其实改的不是很多了。
5. 参考文献
上面的代码insert和delete函数还可以进行优化,比如insert函数假如已经存在某个key了,我们直接return。对于delete函数如果不存在某个key,我们也可以直接return,或者抛出异常。另一方面为了下面的显示我们都是将成员变量设置为public了,但实际中最好是private属性,然后通过自定义get函数获取这些成员变量,我这里为了后面打印和方面,直接全部设置为public了。大家可以进一步优化代码结构。
6. 扩展
再看左神的视频时候,也看到了布隆过滤器和一致性哈希,感觉也是一些有意思的技术,这里也就不详细介绍了,只是列举一些比较好的参考文献。也算是以后面试的时候可以找到吧。