Advertisement
ogv

Untitled

ogv
Nov 17th, 2019
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.64 KB | None | 0 0
  1. // Runtime: 23 ms, faster than 94.80% of Java online submissions for Design HashMap.
  2. // Memory Usage: 50.9 MB, less than 97.30% of Java online submissions for Design HashMap.
  3. class MyHashMap {
  4.     int p;
  5.     int size;
  6.     Entry[] table;
  7.    
  8.     /** Initialize your data structure here. */
  9.     public MyHashMap() {
  10.         p = 37;
  11.         size = 0;
  12.         table = new Entry[p];
  13.     }
  14.    
  15.     /** value will always be non-negative. */
  16.     public void put(int key, int value) {
  17.         int hash = key % p;        
  18.         Entry entry = table[hash];
  19.        
  20.         while (entry != null) {
  21.             if (entry.key == key) {
  22.                 entry.value = value;
  23.                 return;
  24.             }
  25.             entry = entry.next;
  26.         }
  27.        
  28.         if (size >= p) {
  29.             resize();
  30.             hash = key % p;                      
  31.         }
  32.        
  33.         entry = new Entry(key, value, table[hash]);
  34.         table[hash] = entry;
  35.         size++;
  36.     }
  37.    
  38.     private void resize() {
  39.         Entry[] oldTable = table;
  40.        
  41.         p = 2*p + 1;
  42.         size = 0;
  43.         table = new Entry[p];
  44.        
  45.         for (Entry entry: oldTable) {
  46.             while (entry != null) {
  47.                 put(entry.key, entry.value);
  48.                 entry = entry.next;
  49.             }            
  50.         }
  51.     }
  52.    
  53.     /** Returns the value to which the specified key is mapped, or -1 if this map contains no mapping for the key */
  54.     public int get(int key) {
  55.         int hash = key % p;
  56.         Entry entry = table[hash];
  57.        
  58.         while (entry != null) {
  59.             if (entry.key == key) return entry.value;
  60.             entry = entry.next;
  61.         }
  62.        
  63.         return -1;
  64.     }
  65.    
  66.     /** Removes the mapping of the specified value key if this map contains a mapping for the key */
  67.     public void remove(int key) {
  68.         int hash = key % p;
  69.         Entry entry = table[hash];
  70.        
  71.         Entry previous = null;
  72.         while (entry != null) {
  73.             if (entry.key == key) {
  74.                 if (previous == null) table[hash] = entry.next;    
  75.                 else previous.next = entry.next;                
  76.                
  77.                 size--;
  78.                 return;
  79.             }
  80.        
  81.             previous = entry;
  82.             entry = entry.next;
  83.         }                
  84.     }
  85.    
  86.     class Entry {
  87.         public int key;
  88.         public int value;
  89.         public Entry next;
  90.        
  91.         public Entry(int key, int value, Entry next) {
  92.             this.key = key;
  93.             this.value = value;
  94.             this.next = next;
  95.         }
  96.     }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement