-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path705-Design-HashSet.py
More file actions
64 lines (53 loc) · 1.46 KB
/
705-Design-HashSet.py
File metadata and controls
64 lines (53 loc) · 1.46 KB
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
61
62
63
64
# 705. Design HashSet https://leetcode.com/problems/design-hashset/
# level: easy
# complexity:
# Design a HashSet without using any built-in hash table libraries.
class MyHashSet(object):
def __init__(self):
"""
Initialize your data structure here.
"""
self.h = []
def add(self, key):
"""
:type key: int
:rtype: None
"""
if key not in self.h:
self.h.append(key)
def remove(self, key):
"""
:type key: int
:rtype: None
"""
if key not in self.h:
return False
else:
self.h.remove(key)
return True
def contains(self, key):
"""
Returns true if this set contains the specified element
:type key: int
:rtype: bool
"""
if key in self.h:
return True
else:
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)
"""
This problem can also be thought differently, as demonstrated below, though not space efficient
"""
def __init__(self):
self.h = [False] * 1000000 # given the condition of all values will be in the range of [0, 1000000].
def add(self, key):
self.h[key] = True
def remove(self, key):
self.h[key] = False
def contains(self, key):
return self.h[key]