-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDesign HashSet.cs
More file actions
43 lines (37 loc) · 1.04 KB
/
Design HashSet.cs
File metadata and controls
43 lines (37 loc) · 1.04 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
public class MyHashSet {
private bool[][] buckets;
private int k;
private int Hash(int key) {
return key % k;
}
/** Initialize your data structure here. */
public MyHashSet() {
buckets = new bool[1000][];
k = 1000;
}
public void Add(int key) {
var hashKey = Hash(key);
if (buckets[hashKey] == null) {
buckets[hashKey] = new bool[1001];
}
buckets[hashKey][key / k] = true;
}
public void Remove(int key) {
var hashKey = Hash(key);
if (buckets[hashKey] != null) {
buckets[hashKey][key / k] = false;
}
}
/** Returns true if this set contains the specified element */
public bool Contains(int key) {
var hashKey = Hash(key);
return buckets[hashKey] != null && buckets[hashKey][key / k];
}
}
/**
* Your MyHashSet object will be instantiated and called as such:
* MyHashSet obj = new MyHashSet();
* obj.Add(key);
* obj.Remove(key);
* bool param_3 = obj.Contains(key);
*/