forked from super30admin/Design-2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhashmap.py
More file actions
63 lines (49 loc) · 2.06 KB
/
hashmap.py
File metadata and controls
63 lines (49 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# Time Complexity : O(1) for put/get/remove; Worst-case O(n) if many keys collide in one bucket
# Space Complexity : O(n)
# Did this code successfully run on Leetcode : Yes
# Any problem you faced while coding this : Deleting the head of a bucket list and avoiding duplicate keys; solved by using a dummy head per bucket
# Your code here along with comments explaining your approach
# - Use separate chaining: an array of buckets; each bucket holds a singly linked list of (key, value) nodes.
# - Each bucket starts with a dummy (sentinel) node to simplify insert/remove logic at the head.
class ListNode:
def __init__(self, key=-1, val=-1, next=None):
self.key = key
self.val = val
self.next = next
class MyHashMap:
def __init__(self):
# Choose a bucket count; 1000
# Each bucket starts with a dummy/sentinel node so we can always look at .next safely.
self.map = [ListNode() for i in range(1000)]
def hash(self, key):
# Simple modulo hash into [0, bucket_count)
return key % len(self.map)
def put(self, key: int, value: int) -> None:
cur = self.map[self.hash(key)]
# cur is the dummy head; real nodes start at cur.next
while cur.next:
if cur.next.key == key:
cur.next.val = value # Update existing key
return
cur = cur.next
# Key not found: append new node
cur.next = ListNode(key, value)
def get(self, key: int) -> int:
cur = self.map[self.hash(key)].next
while cur:
if cur.key == key:
return cur.val
cur = cur.next
return -1
def remove(self, key: int) -> None:
cur = self.map[self.hash(key)] # start at dummy
while cur and cur.next:
if cur.next.key == key:
cur.next = cur.next.next # unlink the node
return
cur = cur.next
# Your MyHashMap object will be instantiated and called as such:
# obj = MyHashMap()
# obj.put(key,value)
# param_2 = obj.get(key)
# obj.remove(key)