Advertisement
Guest User

Untitled

a guest
Nov 17th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.80 KB | None | 0 0
  1. // Runtime: 27 ms, faster than 92.62% of Java online submissions for Design HashMap.
  2. // Memory Usage: 57.6 MB, less than 35.14% of Java online submissions for Design HashMap.
  3.  
  4. class MyHashMap {
  5.    
  6.     final int CAPACITY = 1000000;
  7.    
  8.     protected int[] storage;
  9.  
  10.     public MyHashMap() {
  11.         this.storage = new int[CAPACITY];
  12.         for (int i = 0; i < CAPACITY - 1; i++) {
  13.             this.storage[i] = -1;
  14.         }
  15.     }
  16.    
  17.     public void put(int key, int value) {
  18.         this.storage[this.hash(key)] = value;
  19.     }
  20.    
  21.     protected int hash(int key) {
  22.         return key % CAPACITY;
  23.     }
  24.    
  25.     public int get(int key) {
  26.         return this.storage[this.hash(key)];
  27.     }
  28.    
  29.     public void remove(int key) {
  30.         this.storage[this.hash(key)] = -1;
  31.     }
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement