classMyHashMap{public:/** Initialize your data structure here. */MyHashMap():lists(kSize){}/** value will always be non-negative. */voidput(intkey,intvalue){auto&pairs=lists[key%kSize];for(auto&[k,v]:pairs)if(k==key){v=value;return;}pairs.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(intkey){constlist<pair<int,int>>&pairs=lists[key%kSize];for(constauto&[k,v]:pairs)if(k==key)returnv;return-1;}/** Removes the mapping of the specified value key if this map contains a * mapping for the key */voidremove(intkey){auto&pairs=lists[key%kSize];for(autoit=begin(pairs);it!=end(pairs);++it)if(it->first==key){pairs.erase(it);return;}}private:staticconstintkSize=10000;vector<list<pair<int,int>>>lists;// Each slot store (key, value) list};
classMyHashMap{/** Initialize your data structure here. */publicMyHashMap(){lists=newList[kSize];for(inti=0;i<kSize;++i)lists[i]=newArrayList<>();}/** value will always be non-negative. */publicvoidput(intkey,intvalue){for(int[]pair:lists[key%kSize])if(pair[0]==key){pair[1]=value;return;}lists[key%kSize].add(newint[]{key,value});}/** * Returns the value to which the specified key is mapped, or -1 if this map * contains no mapping for the key */publicintget(intkey){for(int[]pair:lists[key%kSize])if(pair[0]==key)returnpair[1];return-1;}/** * Removes the mapping of the specified value key if this map contains a mapping * for the key */publicvoidremove(intkey){for(inti=0;i<lists[key%kSize].size();++i)if(lists[key%kSize].get(i)[0]==key){lists[key%kSize].remove(i);return;}}privatestaticfinalintkSize=10000;List<int[]>[]lists;// Each slot store (key, value) list}