Design HashMap
EasyP2Coding
Design a hash map for integer keys and values without using any built-in hash table type (Map, Object as a map, dict, set).
Implement the MyHashMap class:
MyHashMap()creates an empty map.put(key, value)stores the pair; ifkeyalready exists, replace its value.get(key)returns the value forkey, or-1if it is absent.remove(key)deletes the key if present.
Operations should run in O(1) average time.
Example 1
- Input
- ["MyHashMap","put","put","get","get","put","get","remove","get"] [[],[1,1],[2,2],[1],[3],[2,1],[2],[2],[2]]
- Output
- [null,null,null,1,-1,null,1,null,-1]
- Why
- Putting an existing key replaces its value; removed keys read as -1.
Constraints
- 0 <= key, value <= 10^6
- At most 10^4 calls in total.
Hints
Give it a real attempt first. Run shows which examples fail.
Saved automatically
Run checks the 2 visible tests, Submit all 7. ⌘Enter runs, ⌘⇧Enter submits.