Uriel's Corner

Research Associate @ Harvard University / Research Interests: Computer Vision, Biomedical Image Analysis, Machine Learning
posts - 0, comments - 50, trackbacks - 0, articles - 594
模拟哈希表,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)

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理