classMyHashMap { public: /** Initialize your data structure here. */ MyHashMap() { m.resize(n); }
/** value will always be non-negative. */ voidput(int key, int value){ auto it = find(key); if (it != end(m[key % n])) { it->second = value; } else { m[key % n].emplace_back(key, value); } }
/** Returns the value to which the specified key is mapped, or -1 if this map contains no mapping for the key */ intget(int key){ auto it = find(key); return it != end(m[key % n]) ? it->second : -1; }
/** Removes the mapping of the specified value key if this map contains a mapping for the key */ voidremove(int key){ auto it = find(key); if (it != end(m[key % n])) { m[key % n].erase(it); } }
/** * 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); */