拉链法解决冲突
class MyHashMap {
private:
vector<list<pair<int, int>>> data;
static const int num = 706;
int myhash(int n){return n % num;}
public:
MyHashMap() : data(num){
}
void put(int key, int value) {
int index = myhash(key);
auto ite = data[index].begin();
while(ite != data[index].end()){
if((*ite).first == key){
(*ite).second = value;
return;
}
++ite;
}
data[index].push_back({key, value});
}
int get(int key) {
int index = myhash(key);
auto ite = data[index].begin();
while(ite != data[index].end()){
if((*ite).first == key){
return (*ite).second;
}
++ite;
}
return -1;
}
void remove(int key) {
int index = myhash(key);
auto ite = data[index].begin();
while(ite != data[index].end()){
if((*ite).first == key){
data[index].erase(ite);
return;
}
++ite;
}
}
};
/**
* 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);
*/