forked from neetcode-gh/leetcode
- Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0706-design-hashmap.py
36 lines (31 loc) · 1.01 KB
/
0706-design-hashmap.py
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
classListNode:
def__init__(self, key=-1, val=-1, next=None):
self.key=key
self.val=val
self.next=next
classMyHashMap:
def__init__(self):
self.map= [ListNode() foriinrange(1000)]
defhashcode(self, key):
returnkey%len(self.map)
defput(self, key: int, value: int) ->None:
cur=self.map[self.hashcode(key)]
whilecur.next:
ifcur.next.key==key:
cur.next.val=value
return
cur=cur.next
cur.next=ListNode(key, value)
defget(self, key: int) ->int:
cur=self.map[self.hashcode(key)].next
whilecurandcur.key!=key:
cur=cur.next
ifcur:
returncur.val
return-1
defremove(self, key: int) ->None:
cur=self.map[self.hashcode(key)]
whilecur.nextandcur.next.key!=key:
cur=cur.next
ifcurandcur.next:
cur.next=cur.next.next