leetcodeSolution to some Leetcode problems written in C++ |
git clone git://git.dimitrijedobrota.com/leetcode.git |
Log | Files | Refs | README | LICENSE |
0706.cpp (808B)
0 class MyHashMap { 1 const int mod = 9973; 2 3 vector<vector<pair<int, int>>> hm; 4 int hash(int key) { return key % mod; } 5 6 int &find(int key) { 7 static int err = -1; 8 for (auto &[k, v] : hm[hash(key)]) 9 if (k == key) return v; 10 return err; 11 } 12 13 public: 14 MyHashMap() : hm(mod) {} 15 void put(int key, int value) { 16 int &loc = find(key); 17 if (loc == -1) 18 hm[hash(key)].push_back({key, value}); 19 else 20 loc = value; 21 } 22 int get(int key) { return find(key); } 23 void remove(int key) { 24 vector<pair<int, int>> &row = hm[hash(key)]; 25 for (int i = 0; i < row.size(); i++) 26 if (row[i].first == key) { 27 row.erase(row.begin() + i); 28 break; 29 } 30 } 31 };