forked from super30admin/Design-1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhashset.py
49 lines (37 loc) · 889 Bytes
/
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
'''
Time Complexity :
add : O(1)
remove : O(n)
contains : O(n)
Space Complexity : O(n)
Did this code successfully run on Leetcode : yes
Any problem you faced while coding this : No
'''
class MyHashSet(object):
def __init__(self):
self.hash=[]
def add(self, key):
"""
:type key: int
:rtype: None
"""
self.hash.append(key)
def remove(self, key):
"""
:type key: int
:rtype: None
"""
self.hash = [x for x in self.hash if x != key]
def contains(self, key):
"""
:type key: int
:rtype: bool
"""
if key in self.hash:
return True
return False
# Your MyHashSet object will be instantiated and called as such:
# obj = MyHashSet()
# obj.add(key)
# obj.remove(key)
# param_3 = obj.contains(key)