|
| 1 | +# Time: O(1) |
| 2 | +# Space: O(n) |
| 3 | + |
| 4 | +# Design a HashMap without using any built-in hash table libraries. |
| 5 | +# |
| 6 | +# To be specific, your design should include these two functions: |
| 7 | +# |
| 8 | +# put(key, value) : Insert a (key, value) pair into the HashMap. |
| 9 | +# If the value already exists in the HashMap, update the value. |
| 10 | +# get(key): Returns the value to which the specified key is mapped, |
| 11 | +# or -1 if this map contains no mapping for the key. |
| 12 | +# remove(key) : Remove the mapping for the value key if this map contains the mapping for the key. |
| 13 | +# |
| 14 | +# Example: |
| 15 | +# |
| 16 | +# MyHashMap hashMap = new MyHashMap(); |
| 17 | +# hashMap.put(1, 1); |
| 18 | +# hashMap.put(2, 2); |
| 19 | +# hashMap.get(1); // returns 1 |
| 20 | +# hashMap.get(3); // returns -1 (not found) |
| 21 | +# hashMap.put(2, 1); // update the existing value |
| 22 | +# hashMap.get(2); // returns 1 |
| 23 | +# hashMap.remove(2); // remove the mapping for 2 |
| 24 | +# hashMap.get(2); // returns -1 (not found) |
| 25 | +# |
| 26 | +# Note: |
| 27 | +# - All values will be in the range of [1, 1000000]. |
| 28 | +# - The number of operations will be in the range of [1, 10000]. |
| 29 | +# - Please do not use the built-in HashMap library. |
| 30 | + |
| 31 | + |
| 32 | +class ListNode(object): |
| 33 | + def __init__(self, key, val): |
| 34 | + self.val = val |
| 35 | + self.key = key |
| 36 | + self.next = None |
| 37 | + self.prev = None |
| 38 | + |
| 39 | + |
| 40 | +class LinkedList(object): |
| 41 | + def __init__(self): |
| 42 | + self.head = None |
| 43 | + self.tail = None |
| 44 | + |
| 45 | + def insert(self, node): |
| 46 | + node.next, node.prev = None, None # avoid dirty node |
| 47 | + if self.head is None: |
| 48 | + self.head = node |
| 49 | + else: |
| 50 | + self.tail.next = node |
| 51 | + node.prev = self.tail |
| 52 | + self.tail = node |
| 53 | + |
| 54 | + def delete(self, node): |
| 55 | + if node.prev: |
| 56 | + node.prev.next = node.next |
| 57 | + else: |
| 58 | + self.head = node.next |
| 59 | + if node.next: |
| 60 | + node.next.prev = node.prev |
| 61 | + else: |
| 62 | + self.tail = node.prev |
| 63 | + node.next, node.prev = None, None # make node clean |
| 64 | + |
| 65 | + def find(self, key): |
| 66 | + curr = self.head |
| 67 | + while curr: |
| 68 | + if curr.key == key: |
| 69 | + break |
| 70 | + curr = curr.next |
| 71 | + return curr |
| 72 | + |
| 73 | + |
| 74 | +class MyHashMap(object): |
| 75 | + |
| 76 | + def __init__(self): |
| 77 | + """ |
| 78 | + Initialize your data structure here. |
| 79 | + """ |
| 80 | + self.__data = [LinkedList() for _ in xrange(10000)] |
| 81 | + |
| 82 | + def put(self, key, value): |
| 83 | + """ |
| 84 | + value will always be positive. |
| 85 | + :type key: int |
| 86 | + :type value: int |
| 87 | + :rtype: void |
| 88 | + """ |
| 89 | + l = self.__data[key % len(self.__data)] |
| 90 | + node = l.find(key) |
| 91 | + if node: |
| 92 | + node.val = value |
| 93 | + else: |
| 94 | + l.insert(ListNode(key, value)) |
| 95 | + |
| 96 | + def get(self, key): |
| 97 | + """ |
| 98 | + Returns the value to which the specified key is mapped, or -1 if this map contains no mapping for the key |
| 99 | + :type key: int |
| 100 | + :rtype: int |
| 101 | + """ |
| 102 | + l = self.__data[key % len(self.__data)] |
| 103 | + node = l.find(key) |
| 104 | + if node: |
| 105 | + return node.val |
| 106 | + else: |
| 107 | + return -1 |
| 108 | + |
| 109 | + def remove(self, key): |
| 110 | + """ |
| 111 | + Removes the mapping of the specified value key if this map contains a mapping for the key |
| 112 | + :type key: int |
| 113 | + :rtype: void |
| 114 | + """ |
| 115 | + l = self.__data[key % len(self.__data)] |
| 116 | + node = l.find(key) |
| 117 | + if node: |
| 118 | + l.delete(node) |
| 119 | + |
| 120 | + |
| 121 | +# Your MyHashMap object will be instantiated and called as such: |
| 122 | +# obj = MyHashMap() |
| 123 | +# obj.put(key,value) |
| 124 | +# param_2 = obj.get(key) |
| 125 | +# obj.remove(key) |
0 commit comments