-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathLeetcode_DesignBitset.cpp
More file actions
98 lines (89 loc) · 1.61 KB
/
Leetcode_DesignBitset.cpp
File metadata and controls
98 lines (89 loc) · 1.61 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*
2166. Design Bitset
Input
["Bitset", "fix", "fix", "flip", "all", "unfix", "flip", "one", "unfix", "count", "toString"]
[[5], [3], [1], [], [], [0], [], [], [0], [], []]
Output
[null, null, null, null, false, null, null, true, null, 2, "01010"]
*/
class Bitset
{
int *bits;
int *flippedBits;
int size;
int countOne = 0;
public:
Bitset(int size)
{
this->size = size;
bits = new int[size];
flippedBits = new int[size];
for (int i = 0; i < size; i++)
{
bits[i] = 0;
flippedBits[i] = 1;
}
}
void fix(int idx)
{
if (bits[idx])
{
return;
}
else
{
bits[idx] = 1;
flippedBits[idx] = 0;
countOne++;
}
}
void unfix(int idx)
{
if (bits[idx])
{
bits[idx] = 0;
flippedBits[idx] = 1;
countOne--;
}
else
{
return;
}
}
void flip()
{
int *temp = bits;
bits = flippedBits;
flippedBits = temp;
countOne = size - countOne;
}
bool all()
{
if (countOne == size)
{
return true;
}
return false;
}
bool one()
{
if (countOne >= 1)
{
return true;
}
return false;
}
int count()
{
return countOne;
}
string toString()
{
string ans;
for (int i = 0; i < size; i++)
{
ans += to_string(bits[i]);
}
return ans;
}
};