forked from RajwardhanShinde/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathDesignHashSet.cpp
41 lines (37 loc) · 902 Bytes
/
DesignHashSet.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
class MyHashSet {
public:
/** Initialize your data structure here. */
vector<int> v;
MyHashSet() {
}
void add(int key) {
if(v.empty())
v.push_back(key);
for(int i = 0; i < v.size(); i++) {
if(v[i] == key) {
v[i] = key;
return;
}
}
v.push_back(key);
}
void remove(int key) {
if(v.empty())
return;
for(int i = 0; i < v.size(); i++) {
if(v[i] == key) {
v.erase(v.begin() + i);
return;
}
}
}
/** Returns true if this set contains the specified element */
bool contains(int key) {
if(v.empty())
return false;
for(int i = 0; i < v.size(); i++)
if(v[i] == key)
return true;
return false;
}
};