題目: LeetCode - 705. Design HashSet

題目說明

設計一個 Hash_set 的 Class。

解題思路

使用 Unordered_set 實作即可。

參考解法

c++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// fast IO
static auto __ = []()
{
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cout.tie(nullptr);
return 0;
}();
class MyHashSet {
public:
/** Initialize your data structure here. */
MyHashSet() {}

void add(int key) { hs.insert(key); }

void remove(int key) { hs.erase(key); }

/** Returns true if this set contains the specified element */
bool contains(int key) { return hs.count(key); }
private:
unordered_set<int> hs;
};