-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDesign HashSet.cpp
More file actions
43 lines (31 loc) · 1.02 KB
/
Design HashSet.cpp
File metadata and controls
43 lines (31 loc) · 1.02 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
class MyHashSet {
public:
/** Initialize your data structure here. */
array<vector<int>,10000> arrs;
MyHashSet() {
}
void add(int key) {
auto res=find(arrs[key%10000+1].begin(), arrs[key%10000+1].end(), key);
if (res==arrs[key%10000+1].end()) (arrs[key%10000+1]).push_back(key);
}
void remove(int key) {
auto res=find(arrs[key%10000+1].begin(), arrs[key%10000+1].end(), key);
if (res==arrs[key%10000+1].end()) return ;
else
*res=-1;
}
/** Returns true if this set contains the specified element */
bool contains(int key) {
auto res=find(arrs[key%10000+1].begin(), arrs[key%10000+1].end(), key);
if (res==arrs[key%10000+1].end()) return false;
else
return true;
}
};
/**
* 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);
*/