forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdesign-hashset.cpp
45 lines (37 loc) · 1.04 KB
/
design-hashset.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
// Time: O(1)
// Space: O(n)
class MyHashSet {
public:
/** Initialize your data structure here. */
MyHashSet() : data_(10000) {
}
void add(int key) {
auto& list = data_[key % data_.size()];
auto it = find(list.begin(), list.end(), key);
if (it == list.end()) {
list.emplace_back(key);
}
}
void remove(int key) {
auto& list = data_[key % data_.size()];
auto it = find(list.begin(), list.end(), key);
if (it != list.end()) {
list.erase(it);
}
}
/** Returns true if this set did not already contain the specified element */
bool contains(int key) {
auto& list = data_[key % data_.size()];
auto it = find(list.begin(), list.end(), key);
return it != list.end();
}
private:
vector<list<int>> data_;
};
/**
* 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);
*/