/
44_RandomPool.cpp
65 lines (56 loc) · 1.73 KB
/
44_RandomPool.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <iostream>
#include <string>
#include <unordered_map>
#include <ctime>
using namespace std;
class RandomPool {
public:
RandomPool() {
this->size = 0;
}
void insert(string k) {
if ((keyIndexMap.find(k) == keyIndexMap.end()))
{
keyIndexMap[k] = size;
indexKeyMap[size] = k;
size++;
}
}
string getRandom() {
if (this->size == 0)
{
return nullptr;
}
int randomIndex = static_cast<int> (rand() % this->size);
return indexKeyMap[randomIndex];
}
void remove(string key) {
if ((keyIndexMap.find(key) != keyIndexMap.end())) {
int deleteIndex = keyIndexMap[key]; // key, deletIndex为要删除的点
int lastIndex = --this->size; // 最后插入元素的index
string lastKey = indexKeyMap[lastIndex]; // 最后插入的元素
keyIndexMap[lastKey] = deleteIndex; // 修改最后一个key对应的index
indexKeyMap[deleteIndex] = lastKey; // 填坑
keyIndexMap.erase(key); // keyIndexMap删除key
indexKeyMap.erase(lastIndex); // indexKeyMap删除最后一个元素
}
}
private:
unordered_map<string, int> keyIndexMap;
unordered_map<int, string> indexKeyMap;
int size;
};
int main()
{
srand(static_cast<int>(time(NULL)));
RandomPool pool;
pool.insert("zyb");
pool.insert("haha");
pool.insert("hehe");
pool.remove("hehe");
pool.remove("haha");
printf("%s\n", pool.getRandom().c_str());
printf("%s\n", pool.getRandom().c_str());
printf("%s\n", pool.getRandom().c_str());
return 0;
}