-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDesignHashSet.java
More file actions
43 lines (34 loc) · 902 Bytes
/
Copy pathDesignHashSet.java
File metadata and controls
43 lines (34 loc) · 902 Bytes
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
/*
LeetCode 705 - Design HashSet
Implement a HashSet without using built-in hash table libraries.
Methods:
- add(key): Insert key into the set.
- remove(key): Remove key from the set.
- contains(key): Return true if key exists, otherwise false.
*/
class MyHashSet {
// Constraints: 0 <= key <= 10^6
private boolean[] set;
public MyHashSet() {
set = new boolean[1000001];
}
// Add the key to the HashSet
public void add(int key) {
set[key] = true;
}
// Remove the key from the HashSet
public void remove(int key) {
set[key] = false;
}
// Check whether the key exists
public boolean contains(int key) {
return set[key];
}
}
/**
* Your MyHashSet object will be instantiated and called as such:
* MyHashSet obj = new MyHashSet();
* obj.add(key);
* obj.remove(key);
* boolean param_3 = obj.contains(key);
*/