- Notifications
You must be signed in to change notification settings - Fork 366
/
Copy pathdesign-hashmap.cpp
37 lines (33 loc) · 988 Bytes
/
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
classMyHashMap {
public:
MyHashMap() {
}
vector<pair<int,int>> bucket[100]; //bucket is an array whose each element is a vector of pair
voidput(int key, int value) {
intindex = key%100;
for(int i=0 ; i<bucket[index].size(); i++){
if(bucket[index][i].first==key){
bucket[index][i].second=value;
return;
}
}
bucket[index].push_back({key,value});
}
intget(int key) {
intindex = key%100;
for(int i=0 ; i<bucket[index].size(); i++){
if(bucket[index][i].first==key)
return bucket[index][i].second;
}
return -1;
}
voidremove(int key) {
intindex = key%100;
for(int i=0 ; i<bucket[index].size(); i++){
if(bucket[index][i].first==key){
bucket[index].erase(bucket[index].begin() + i);
return;
}
}
}
};