Skip to content

Commit e7a692d

Browse files
committed
feat: add solutions to lc problems: No.0705,0706
* No.0705.Design HashSet * No.0706.Design HashMap
1 parent 118dc56 commit e7a692d

File tree

11 files changed

+681
-84
lines changed

11 files changed

+681
-84
lines changed

solution/0700-0799/0705.Design HashSet/README.md

+232-36
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,17 @@ myHashSet.contains(2); // 返回 False ,(已移除)</pre>
5050

5151
<!-- 这里可写通用的实现逻辑 -->
5252

53-
数组实现。
53+
**方法一:静态数组实现**
54+
55+
直接创建一个大小为 $1000001$ 的数组,初始时数组中的每个元素都为 `false`,表示哈希集合中不存在该元素。
56+
57+
往哈希集合添加元素时,将数组中对应位置的值置为 `true`;删除元素时,将数组中对应位置的值置为 `false`;当查询元素是否存在时,直接返回数组中对应位置的值即可。
58+
59+
以上操作的时间复杂度均为 $O(1)$。
60+
61+
**方法二:数组嵌套链表**
62+
63+
我们也可以开辟一个大小为 `SIZE=1000` 的数组,数组的每个位置是一个链表。
5464

5565
<!-- tabs:start -->
5666

@@ -60,10 +70,8 @@ myHashSet.contains(2); // 返回 False ,(已移除)</pre>
6070

6171
```python
6272
class MyHashSet:
73+
6374
def __init__(self):
64-
"""
65-
Initialize your data structure here.
66-
"""
6775
self.data = [False] * 1000001
6876

6977
def add(self, key: int) -> None:
@@ -73,12 +81,43 @@ class MyHashSet:
7381
self.data[key] = False
7482

7583
def contains(self, key: int) -> bool:
76-
"""
77-
Returns true if this set contains the specified element
78-
"""
7984
return self.data[key]
8085

8186

87+
# Your MyHashSet object will be instantiated and called as such:
88+
# obj = MyHashSet()
89+
# obj.add(key)
90+
# obj.remove(key)
91+
# param_3 = obj.contains(key)
92+
```
93+
94+
```python
95+
class MyHashSet:
96+
97+
def __init__(self):
98+
self.size = 1000
99+
self.data = [[] for _ in range(self.size)]
100+
101+
def add(self, key: int) -> None:
102+
if self.contains(key):
103+
return
104+
idx = self.hash(key)
105+
self.data[idx].append(key)
106+
107+
def remove(self, key: int) -> None:
108+
if not self.contains(key):
109+
return
110+
idx = self.hash(key)
111+
self.data[idx].remove(key)
112+
113+
def contains(self, key: int) -> bool:
114+
idx = self.hash(key)
115+
return any(v == key for v in self.data[idx])
116+
117+
def hash(self, key) -> int:
118+
return key % self.size
119+
120+
82121
# Your MyHashSet object will be instantiated and called as such:
83122
# obj = MyHashSet()
84123
# obj.add(key)
@@ -90,16 +129,12 @@ class MyHashSet:
90129

91130
<!-- 这里可写当前语言的特殊实现逻辑 -->
92131

93-
- 可以一次性开辟一个大的数组,存放所有元素。
94-
95132
```java
96133
class MyHashSet {
134+
private boolean[] data = new boolean[1000001];
97135

98-
private boolean[] data;
99-
100-
/** Initialize your data structure here. */
101136
public MyHashSet() {
102-
data = new boolean[1000001];
137+
103138
}
104139

105140
public void add(int key) {
@@ -110,7 +145,6 @@ class MyHashSet {
110145
data[key] = false;
111146
}
112147

113-
/** Returns true if this set contains the specified element */
114148
public boolean contains(int key) {
115149
return data[key];
116150
}
@@ -125,46 +159,42 @@ class MyHashSet {
125159
*/
126160
```
127161

128-
- 也可以开辟一个大小为 `SIZE` 的数组,数组的每个位置是一个链表。
129-
130162
```java
131163
class MyHashSet {
132-
133164
private static final int SIZE = 1000;
134165
private LinkedList[] data;
135166

136-
/** Initialize your data structure here. */
137167
public MyHashSet() {
138168
data = new LinkedList[SIZE];
139-
Arrays.fill(data, new LinkedList<Integer>());
169+
for (int i = 0; i < SIZE; ++i) {
170+
data[i] = new LinkedList<Integer>();
171+
}
140172
}
141173

142174
public void add(int key) {
143-
int index = hash(key);
144-
Iterator<Integer> iterator = data[index].iterator();
145-
while (iterator.hasNext()) {
146-
Integer e = iterator.next();
147-
if (e == key) return;
175+
if (contains(key)) {
176+
return;
148177
}
149-
data[index].addFirst(key);
178+
int idx = hash(key);
179+
data[idx].addFirst(key);
150180
}
151181

152182
public void remove(int key) {
153-
int index = hash(key);
154-
ListIterator<Integer> iterator = data[index].listIterator();
155-
while (iterator.hasNext()) {
156-
Integer e = iterator.next();
157-
if (e == key) iterator.remove();
183+
if (!contains(key)) {
184+
return;
158185
}
186+
int idx = hash(key);
187+
data[idx].remove(Integer.valueOf(key));
159188
}
160189

161-
/** Returns true if this set contains the specified element */
162190
public boolean contains(int key) {
163-
int index = hash(key);
164-
Iterator<Integer> iterator = data[index].iterator();
165-
while (iterator.hasNext()) {
166-
Integer e = iterator.next();
167-
if (e == key) return true;
191+
int idx = hash(key);
192+
Iterator<Integer> it = data[idx].iterator();
193+
while (it.hasNext()) {
194+
Integer e = it.next();
195+
if (e == key) {
196+
return true;
197+
}
168198
}
169199
return false;
170200
}
@@ -183,6 +213,172 @@ class MyHashSet {
183213
*/
184214
```
185215

216+
### **C++**
217+
218+
```cpp
219+
class MyHashSet {
220+
public:
221+
bool data[1000001];
222+
223+
MyHashSet() {
224+
memset(data, false, sizeof data);
225+
}
226+
227+
void add(int key) {
228+
data[key] = true;
229+
}
230+
231+
void remove(int key) {
232+
data[key] = false;
233+
}
234+
235+
bool contains(int key) {
236+
return data[key];
237+
}
238+
};
239+
240+
/**
241+
* Your MyHashSet object will be instantiated and called as such:
242+
* MyHashSet* obj = new MyHashSet();
243+
* obj->add(key);
244+
* obj->remove(key);
245+
* bool param_3 = obj->contains(key);
246+
*/
247+
```
248+
249+
```cpp
250+
class MyHashSet {
251+
private:
252+
int size = 1000;
253+
vector<list<int>> data;
254+
255+
public:
256+
MyHashSet(): data(size) {
257+
258+
}
259+
260+
void add(int key) {
261+
if (contains(key)) {
262+
return;
263+
}
264+
int idx = hash(key);
265+
data[idx].push_back(key);
266+
}
267+
268+
void remove(int key) {
269+
if (!contains(key)) {
270+
return;
271+
}
272+
int idx = hash(key);
273+
data[idx].remove(key);
274+
}
275+
276+
bool contains(int key) {
277+
int idx = hash(key);
278+
for (auto it = data[idx].begin(); it != data[idx].end(); it++) {
279+
if ((*it) == key) {
280+
return true;
281+
}
282+
}
283+
return false;
284+
}
285+
286+
int hash(int key) {
287+
return key % size;
288+
}
289+
};
290+
291+
/**
292+
* Your MyHashSet object will be instantiated and called as such:
293+
* MyHashSet* obj = new MyHashSet();
294+
* obj->add(key);
295+
* obj->remove(key);
296+
* bool param_3 = obj->contains(key);
297+
*/
298+
```
299+
300+
### **Go**
301+
302+
```go
303+
type MyHashSet struct {
304+
data []bool
305+
}
306+
307+
func Constructor() MyHashSet {
308+
data := make([]bool, 1000010)
309+
return MyHashSet{data}
310+
}
311+
312+
func (this *MyHashSet) Add(key int) {
313+
this.data[key] = true
314+
}
315+
316+
func (this *MyHashSet) Remove(key int) {
317+
this.data[key] = false
318+
}
319+
320+
func (this *MyHashSet) Contains(key int) bool {
321+
return this.data[key]
322+
}
323+
324+
/**
325+
* Your MyHashSet object will be instantiated and called as such:
326+
* obj := Constructor();
327+
* obj.Add(key);
328+
* obj.Remove(key);
329+
* param_3 := obj.Contains(key);
330+
*/
331+
```
332+
333+
```go
334+
type MyHashSet struct {
335+
data []list.List
336+
}
337+
338+
func Constructor() MyHashSet {
339+
return MyHashSet{make([]list.List, 1000)}
340+
}
341+
342+
func (this *MyHashSet) Add(key int) {
343+
if this.Contains(key) {
344+
return
345+
}
346+
idx := this.hash(key)
347+
this.data[idx].PushBack(key)
348+
}
349+
350+
func (this *MyHashSet) Remove(key int) {
351+
idx := this.hash(key)
352+
for e := this.data[idx].Front(); e != nil; e = e.Next() {
353+
if e.Value.(int) == key {
354+
this.data[idx].Remove(e)
355+
}
356+
}
357+
}
358+
359+
func (this *MyHashSet) Contains(key int) bool {
360+
idx := this.hash(key)
361+
for e := this.data[idx].Front(); e != nil; e = e.Next() {
362+
if e.Value.(int) == key {
363+
return true
364+
}
365+
}
366+
return false
367+
}
368+
369+
func (this *MyHashSet) hash(key int) int {
370+
return key % len(this.data)
371+
}
372+
373+
/**
374+
* Your MyHashSet object will be instantiated and called as such:
375+
* obj := Constructor();
376+
* obj.Add(key);
377+
* obj.Remove(key);
378+
* param_3 := obj.Contains(key);
379+
*/
380+
```
381+
186382
### **TypeScript**
187383

188384
```ts

0 commit comments

Comments
 (0)