Design HashMap

Design a HashMap without using any built-in hash table libraries.

To be specific, your design should include these functions:

  • put(key, value) : Insert a (key, value) pair into the HashMap. If the value already exists in the HashMap, update the value.
  • get(key): Returns the value to which the specified key is mapped, or -1 if this map contains no mapping for the key.
  • remove(key) : Remove the mapping for the value key if this map contains the mapping for the key.


Example:

MyHashMap hashMap = new MyHashMap();
hashMap.put(1, 1);          
hashMap.put(2, 2);         
hashMap.get(1);            // returns 1
hashMap.get(3);            // returns -1 (not found)
hashMap.put(2, 1);          // update the existing value
hashMap.get(2);            // returns 1 
hashMap.remove(2);          // remove the mapping for 2
hashMap.get(2);            // returns -1 (not found) 

 1 class MyHashMap {
 2     final ListNode[] nodes = new ListNode[10000];
 3 
 4     public void put(int key, int value) {
 5         int i = idx(key);
 6         ListNode first = nodes[i];
 7         ListNode newNode = new ListNode(key, value);
 8         if (first == null) {
 9             nodes[i] = newNode;
10         } else {
11             ListNode sameNode = find(nodes[i], key);
12             if (sameNode == null) {
13                 newNode.next = first;
14                 nodes[i] = newNode;
15             } else {
16                 sameNode.val = value;
17             }
18         }
19     }
20 
21     public int get(int key) {
22         int i = idx(key);
23         if (nodes[i] == null) {
24             return -1;
25         }
26         ListNode node = find(nodes[i], key);
27         return node == null ? -1 : node.val;
28     }
29 
30     public void remove(int key) {
31         int i = idx(key);
32         if (nodes[i] == null) {
33             return;
34         }
35         ListNode current = nodes[i];
36         ListNode previous = null;
37         while (current != null) {
38             if (current.key == key) {
39                 if (previous != null) {
40                     previous.next = current.next;
41                 } else {
42                     nodes[i] = current.next;
43                 }
44                 break;
45             } else {
46                 previous = current;
47                 current = current.next;
48             }
49         }
50     }
51 
52     int idx(int key) {
53         return Integer.hashCode(key) % nodes.length;
54     }
55 
56     ListNode find(ListNode node, int key) {
57         while (node != null) {
58             if (node.key == key) {
59                 return node;
60             }
61             node = node.next;
62         }
63         return null;
64     }
65 
66     class ListNode {
67         int key, val;
68         ListNode next;
69 
70         ListNode(int key, int val) {
71             this.key = key;
72             this.val = val;
73         }
74     }
75 }

 Python version

 1 class MyHashMap:
 2     def __init__(self):
 3         self.size = 1000
 4         self.buckets = [[] for _ in range(self.size)]
 5         
 6     def _hash(self, key: int) -> int:
 7         return key % self.size
 8 
 9     def put(self, key: int, value: int) -> None:
10         idx = self._hash(key)
11         bucket = self.buckets[idx]
12 
13         for i, (k, v) in enumerate(bucket):
14             if k == key:
15                 bucket[i] = (key, value)  # update
16                 return
17 
18         bucket.append((key, value))  # insert
19         
20 
21     def get(self, key: int) -> int:
22         idx = self._hash(key)
23         bucket = self.buckets[idx]
24 
25         for k, v in bucket:
26             if k == key:
27                 return v
28         return -1
29         
30 
31     def remove(self, key: int) -> None:
32         idx = self._hash(key)
33         bucket = self.buckets[idx]
34 
35         for i, (k, v) in enumerate(bucket):
36             if k == key:
37                 bucket.pop(i)
38                 return
39 
40 
41 # Your MyHashMap object will be instantiated and called as such:
42 # obj = MyHashMap()
43 # obj.put(key,value)
44 # param_2 = obj.get(key)
45 # obj.remove(key)

 



posted @ 2019-07-15 10:54  北叶青藤  阅读(216)  评论(0)    收藏  举报