Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Runtime: 27 ms, faster than 92.62% of Java online submissions for Design HashMap.
- // Memory Usage: 57.6 MB, less than 35.14% of Java online submissions for Design HashMap.
- class MyHashMap {
- final int CAPACITY = 1000000;
- protected int[] storage;
- public MyHashMap() {
- this.storage = new int[CAPACITY];
- for (int i = 0; i < CAPACITY - 1; i++) {
- this.storage[i] = -1;
- }
- }
- public void put(int key, int value) {
- this.storage[this.hash(key)] = value;
- }
- protected int hash(int key) {
- return key % CAPACITY;
- }
- public int get(int key) {
- return this.storage[this.hash(key)];
- }
- public void remove(int key) {
- this.storage[this.hash(key)] = -1;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement