Advertisement
ogv

Untitled

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