-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDesignHashSet.cpp
More file actions
29 lines (29 loc) · 1 KB
/
Copy pathDesignHashSet.cpp
File metadata and controls
29 lines (29 loc) · 1 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
class MyHashSet {public:
int M; //size
vector<list<int>>buckets;
int getIndex(int key){
return key%M;
}
MyHashSet() {
M=15000;
buckets=vector<list<int>>(M,list<int>{});
}
void add(int key) {
int index=getIndex(key);
auto itr=find(buckets[index].begin(),buckets[index].end(),key);
if(itr==buckets[index].end()){
buckets[index].push_back(key);
}
}
void remove(int key) {
int index=getIndex(key);
auto itr=find(buckets[index].begin(),buckets[index].end(),key);
if(itr!=buckets[index].end()){
buckets[index].erase(itr);
}
}
bool contains(int key) {
int index=getIndex(key);
auto itr=find(buckets[index].begin(),buckets[index].end(),key);
return itr!=buckets[index].end();
}};/** * 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); */