-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLC2.CPP
More file actions
43 lines (37 loc) · 1.08 KB
/
LC2.CPP
File metadata and controls
43 lines (37 loc) · 1.08 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
//Design Hash set
//Problem link : https://leetcode.com/explore/challenge/card/august-leetcoding-challenge/549/week-1-august-1st-august-7th/3410/
#include <bits/stdc++.h>
using namespace std;
class MyHashSet {
public:
/** Initialize your data structure here. */
int ar[1000000];
MyHashSet() {
//constructor
memset(ar, -1, sizeof(ar));
}
void add(int key) {
ar[key]=1;
}
void remove(int key) {
ar[key]=-1;
}
/** Returns true if this set contains the specified element */
bool contains(int key) {
return (ar[key]==1);
}
};
int main()
{
MyHashSet* obj = new MyHashSet();
obj->add(1);
obj->add(2);
cout << obj->contains(1) << endl; // returns true
cout << obj->contains(3) << endl; // returns false (not found)
obj->add(2);
cout << obj->contains(2) << endl; // returns true
obj->remove(2);
cout << obj->contains(2) << endl; // returns false (already removed)
cout << obj->contains(3) << endl;
return 0;
}