-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathDesign HashSet.cpp
56 lines (35 loc) · 1.1 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
46
47
48
49
50
51
52
53
54
55
56
/*
Solution by Rahul Surana
***********************************************************
Design a HashSet without using any built-in hash table libraries.
Implement MyHashSet class:
void add(key) Inserts the value key into the HashSet.
bool contains(key) Returns whether the value key exists in the HashSet or not.
void remove(key) Removes the value key in the HashSet. If key does not exist in the HashSet, do nothing.
***********************************************************
*/
#include <bits/stdc++.h>
class MyHashSet {
public:
set<int> s;
MyHashSet() {
}
void add(int key) {
s.insert(key);
}
void remove(int key) {
auto x = find(s.begin(),s.end(),key);
if(x!=s.end())
s.erase(x);
}
bool contains(int key) {
return s.count(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);
*/