-
Notifications
You must be signed in to change notification settings - Fork 14
/
705-design-hashset.py
60 lines (52 loc) · 1.73 KB
/
705-design-hashset.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
class ListNode:
def __init__(self, val, next=None):
self.val = val
self.next = next
class MyHashSet:
def __init__(self):
self.hash = 1009
self.buckets = [None for _ in range(self.hash)]
def add(self, key: int) -> None:
idx = key % self.hash
if self.buckets[idx] != None:
node = self.buckets[idx]
prev, cur, find = self.check(key, node)
if not find:
self.buckets[idx] = ListNode(key, node)
else:
self.buckets[idx] = ListNode(key)
def remove(self, key: int) -> None:
idx = key % self.hash
if self.buckets[idx] != None:
node = self.buckets[idx]
prev, cur, find = self.check(key, node)
if find:
if prev:
prev.next = cur.next
else:
self.buckets[idx] = cur.next
def contains(self, key: int) -> bool:
idx = key % self.hash
if self.buckets[idx] != None:
node = self.buckets[idx]
prev, cur, find = self.check(key, node)
return find
else:
return False
def check(self, val, node):
prev, cur, find = None, node, False
while cur:
if cur.val == val:
find = True
break
prev = cur
cur = cur.next
return prev, cur, find
# Your MyHashSet object will be instantiated and called as such:
# obj = MyHashSet()
# obj.add(key)
# obj.remove(key)
# param_3 = obj.contains(key)
# time O(n/k), k is the number of buckets, n/k is the length of linked list in bucket
# space O(n + k)
# using hahsmap and separate chaining and linked list and two pointers