-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path706. Design HashMap.cpp
44 lines (40 loc) · 1.01 KB
/
706. Design HashMap.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
class MyHashMap {
public:
vector<list<pair<int,int>>> m_data;
MyHashMap() {
m_data.resize(100000);
}
void put(int key, int value) {
auto &list = m_data[key % 100000];
for (auto & val : list) {
if (val.first == key) {
val.second = value;
return;
}
}
list.emplace_back(key, value);
}
int get(int key) {
const auto &list = m_data[key % 100000];
if (list.empty()) {
return -1;
}
for (const auto & val : list) {
if (val.first == key) {
return val.second;
}
}
return -1;
}
void remove(int key) {
auto &list = m_data[key % 100000];
list.remove_if([key](auto n) { return n.first == key; });
}
};
/**
* Your MyHashMap object will be instantiated and called as such:
* MyHashMap* obj = new MyHashMap();
* obj->put(key,value);
* int param_2 = obj->get(key);
* obj->remove(key);
*/