Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit eba58dc

Browse files
committed
design_hashset.py
1 parent 7cbc38f commit eba58dc

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed

‎DSA_1_Year_Full_Plan.xlsx

16.7 KB
Binary file not shown.

‎arrays_hashing/design_hashset.py

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
"""
2+
LeetCode Problem: Design HashSet
3+
--------------------------------
4+
Design a HashSet without using any built-in hash table libraries.
5+
6+
Implement the MyHashSet class:
7+
8+
Methods:
9+
- add(key): Inserts the value 'key' into the HashSet.
10+
- contains(key): Returns True if 'key' exists in the HashSet, else False.
11+
- remove(key): Removes the value 'key' from the HashSet.
12+
If 'key' does not exist, do nothing.
13+
14+
Example:
15+
Input:
16+
["MyHashSet", "add", "add", "contains", "contains", "add", "contains", "remove", "contains"]
17+
[[], [1], [2], [1], [3], [2], [2], [2], [2]]
18+
19+
Output:
20+
[null, null, null, true, false, null, true, null, false]
21+
22+
Explanation:
23+
myHashSet = MyHashSet()
24+
myHashSet.add(1) # set = [1]
25+
myHashSet.add(2) # set = [1, 2]
26+
myHashSet.contains(1) # returns True
27+
myHashSet.contains(3) # returns False (not found)
28+
myHashSet.add(2) # set = [1, 2]
29+
myHashSet.contains(2) # returns True
30+
myHashSet.remove(2) # set = [1]
31+
myHashSet.contains(2) # returns False (already removed)
32+
33+
Constraints:
34+
- 0 <= key <= 1,000,000
35+
- At most 10,000 calls will be made to add, remove, and contains.
36+
"""
37+
38+
# Method 1:
39+
class MyHashSet:
40+
41+
def __init__(self):
42+
self.data = []
43+
44+
def add(self, key: int) -> None:
45+
if key not in self.data:
46+
self.data.append(key)
47+
48+
def remove(self, key: int) -> None:
49+
if key in self.data:
50+
self.data.remove(key)
51+
52+
def contains(self, key: int) -> bool:
53+
if key in self.data:
54+
return True
55+
else:
56+
return False
57+
58+
# Method 2:
59+
class MyHashSet:
60+
61+
def __init__(self):
62+
self.data = [False]*100001
63+
64+
def add(self, key: int) -> None:
65+
self.data[key] = True
66+
67+
def remove(self, key: int) -> None:
68+
self.data[key] = False
69+
70+
def contains(self, key: int) -> bool:
71+
return self.data[key]
72+
73+
74+
# Method 3:
75+
class MyHashSet:
76+
77+
def __init__(self):
78+
self.size = 10000
79+
self.table = [[] for _ in range(self.size)]
80+
81+
def calculate_hash_value(self,key: int):
82+
return key % self.size
83+
84+
def add(self, key: int) -> None:
85+
hv = self.calculate_hash_value(key)
86+
if key not in self.table[hv]:
87+
self.table[hv].append(key)
88+
89+
def remove(self, key: int) -> None:
90+
hv = self.calculate_hash_value(key)
91+
if key in self.table[hv]:
92+
self.table[hv].remove(key)
93+
94+
def contains(self, key: int) -> bool:
95+
hv = self.calculate_hash_value(key)
96+
return key in self.table[hv]
97+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /