-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy path705. Design HashSet solution.cpp
More file actions
52 lines (47 loc) · 958 Bytes
/
705. Design HashSet solution.cpp
File metadata and controls
52 lines (47 loc) · 958 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
44
45
46
47
48
49
50
51
52
class MyHashSet {
public:
vector<list<int>>v;
int size=11;
MyHashSet() {
v.resize(size);
}
int hash(int key)
{
int i=key%size;
return i;
}
bool search(int key)
{
int i=hash(key);
list<int>::iterator it = find(v[i].begin(),v[i].end(),key);
if(it!=v[i].end())
{
return true;
}
return false;
}
void add(int key) {
int i=hash(key);
if(!search(key))
{
v[i].push_back(key);
}
}
void remove(int key) {
int i=hash(key);
if(search(key))
{
v[i].remove(key);
}
}
bool contains(int key) {
return search(key);
}
};
/**
* 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);
*/