-
Notifications
You must be signed in to change notification settings - Fork 2
/
q0705.py
56 lines (47 loc) · 1.55 KB
/
q0705.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
#!/usr/bin/python3
from typing import List
class MyHashSet:
prime = 19993
hashList = list()
def __init__(self):
self.hashList = [-1 for _ in range(self.prime)]
def add(self, key: int) -> None:
index = key % self.prime
while self.hashList[index] != -1 and self.hashList[index] != key:
index = (1 + index) % self.prime
if self.hashList[index] == key:
return
else:
self.hashList[index] = key
def contains(self, key: int) -> bool:
index = key % self.prime
while self.hashList[index] != -1 and self.hashList[index] != key:
index = (1 + index) % self.prime
if self.hashList[index] == key:
return True
else:
return False
def remove(self, key: int) -> None:
index = key % self.prime
while self.hashList[index] != -1 and self.hashList[index] != key:
index = (1 + index) % self.prime
if self.hashList[index] == -1:
# 没找到退出
return
else:
# 找到了删除
self.hashList[index] = -1
index = (index + 1) % self.prime
while self.hashList[index] != -1:
origin = self.hashList[index]
self.hashList[index] = -1
self.add(origin)
index += 1
# Your MyHashSet object will be instantiated and called as such:
obj = MyHashSet()
obj.add(0)
obj.add(1)
print(obj.contains(1))
print(obj.contains(3))
obj.add(19993)
# param_3 = obj.contains(key)