forked from super30admin/Design-2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhashset.cpp
More file actions
62 lines (52 loc) · 1.26 KB
/
Copy pathhashset.cpp
File metadata and controls
62 lines (52 loc) · 1.26 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/ Time Complexity :all operations T(n) = O(1) ==> add,remove,contains
// Space Complexity :S(n)=O(n)
// Did this code successfully run on Leetcode :yes
// Your code here along with comments explaining your approach
// used double hashing with boolean vector
class MyHashSet {
public:
vector<vector<bool>>map;
int getBucket(int key)
{
return key%1000;
}
int getBucketItem(int key)
{
return key/1000;
}
MyHashSet()
{
this->map.resize(1000);
}
void add(int key)
{
int bucket=getBucket(key);
int bucketItem=getBucketItem(key);
if(map[bucket].size()<bucketItem+1)
{
map[bucket].resize(bucketItem+1,false);
}
map[bucket][bucketItem]=true;
}
void remove(int key)
{
int bucket=getBucket(key);
int bucketItem=getBucketItem(key);
if(map[bucket].size()<bucketItem+1)
{
return;
}
map[bucket][bucketItem]=false;
return;
}
bool contains(int key)
{
int bucket=getBucket(key);
int bucketItem=getBucketItem(key);
if(map[bucket].size()<bucketItem+1)
{
return false;
}
return map[bucket][bucketItem];
}
};