-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path0706.cpp
More file actions
32 lines (29 loc) · 808 Bytes
/
Copy path0706.cpp
File metadata and controls
32 lines (29 loc) · 808 Bytes
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
class MyHashMap {
const int mod = 9973;
vector<vector<pair<int, int>>> hm;
int hash(int key) { return key % mod; }
int &find(int key) {
static int err = -1;
for (auto &[k, v] : hm[hash(key)])
if (k == key) return v;
return err;
}
public:
MyHashMap() : hm(mod) {}
void put(int key, int value) {
int &loc = find(key);
if (loc == -1)
hm[hash(key)].push_back({key, value});
else
loc = value;
}
int get(int key) { return find(key); }
void remove(int key) {
vector<pair<int, int>> &row = hm[hash(key)];
for (int i = 0; i < row.size(); i++)
if (row[i].first == key) {
row.erase(row.begin() + i);
break;
}
}
};