-
Notifications
You must be signed in to change notification settings - Fork 1
/
DesignHashSet_705.cpp
77 lines (55 loc) · 1.17 KB
/
DesignHashSet_705.cpp
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
65
66
67
68
69
70
71
72
73
74
75
76
77
/*
~ Author : leetcode.com/tridib_2003/
~ Problem : 705. Design HashSet
~ Link : https://leetcode.com/problems/design-hashset/
*/
class MyHashSet {
private:
const int size = 200;
vector<vector<int> *> set;
public:
MyHashSet() {
set.resize(size);
}
void add(int key) {
int index = key % size;
if (!contains(key))
{
auto v = set[index];
if (v == nullptr)
v = new vector<int>;
v->push_back(key);
set[index] = v;
}
}
void remove(int key) {
int index = key % size;
auto v = set[index];
if (v == nullptr)
return;
for (auto = v->begin(); iter != v->end(); iter++) {
if (*iter == key) {
v->erase(iter);
return;
}
}
}
bool contains(int key) {
int index = key % size;
auto v = set[index];
if (v == nullptr)
return false;
for (auto e : *v) {
if (e == key)
return true;
}
return false;
}
};
/**
* 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);
*/