Posted on 2023-05-30 21:15
Uriel 阅读(28)
评论(0) 编辑 收藏 引用 所属分类:
闲来无事重切Leet Code 、
Hash
模拟哈希表,python set打卡题
1 #705
2 #Runtime: 129 ms (Beats 88.7%)
3 #Memory: 18.8 MB (Beats 62.84%)
4
5 class MyHashSet(object):
6
7 def __init__(self):
8 self.h_set = set()
9
10 def add(self, key):
11 """
12 :type key: int
13 :rtype: None
14 """
15 self.h_set.add(key)
16
17 def remove(self, key):
18 """
19 :type key: int
20 :rtype: None
21 """
22 if key in self.h_set:
23 self.h_set.remove(key)
24
25 def contains(self, key):
26 """
27 :type key: int
28 :rtype: bool
29 """
30 if key in self.h_set:
31 return True
32 return False
33
34
35
36 # Your MyHashSet object will be instantiated and called as such:
37 # obj = MyHashSet()
38 # obj.add(key)
39 # obj.remove(key)
40 # param_3 = obj.contains(key)