ogv

Untitled

ogv
Nov 17th, 2019
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.50 KB | None | 0 0
  1. // Runtime: 22 ms, faster than 95.40% of Java online submissions for Design HashMap.
  2. // Memory Usage: 50 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.         for (Entry e = table[key % p]; e != null; e = e.next)
  18.             if (e.key == key) {
  19.                 e.value = value;
  20.                 return;
  21.             }
  22.                
  23.         if (size >= p) resize();
  24.        
  25.         insert(new Entry(key, value));        
  26.     }
  27.    
  28.     private void insert(Entry entry) {
  29.         int hash = entry.key % p;
  30.        
  31.         entry.next = table[hash];
  32.         table[hash] = entry;
  33.         size++;
  34.     }
  35.    
  36.     private void resize() {
  37.         Entry[] oldTable = table;
  38.        
  39.         p = 2*p + 1;
  40.         size = 0;
  41.         table = new Entry[p];
  42.        
  43.         for (Entry entry: oldTable)
  44.             while (entry != null) {
  45.                 Entry next = entry.next;
  46.                 insert(entry);                
  47.                 entry = next;
  48.             }        
  49.     }
  50.    
  51.     /** Returns the value to which the specified key is mapped, or -1 if this map contains no mapping for the key */
  52.     public int get(int key) {        
  53.         for (Entry e = table[key % p]; e != null; e = e.next)
  54.             if (e.key == key) return e.value;
  55.        
  56.         return -1;
  57.     }
  58.    
  59.     /** Removes the mapping of the specified value key if this map contains a mapping for the key */
  60.     public void remove(int key) {
  61.         int hash = key % p;
  62.         Entry entry = table[hash];
  63.        
  64.         Entry previous = null;
  65.         while (entry != null) {
  66.             if (entry.key == key) {
  67.                 if (previous == null) table[hash] = entry.next;    
  68.                 else previous.next = entry.next;                
  69.                
  70.                 size--;
  71.                 return;
  72.             }
  73.        
  74.             previous = entry;
  75.             entry = entry.next;
  76.         }                
  77.     }
  78.    
  79.     class Entry {
  80.         public int key;
  81.         public int value;
  82.         public Entry next;
  83.        
  84.         public Entry(int key, int value) {
  85.             this.key = key;
  86.             this.value = value;
  87.         }
  88.     }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment