Design Bitset LeetCode solution in C++

1
class Bitset {
2
public:
3
vector<int> arr;
4
int cnt, cntflip;
5
Bitset(int size) {
6
arr.resize(size, 0);
7
cnt = 0, cntflip = 0;
8
}
9
void fix(int idx) {
10
// means current bit is 0 ,so set it to 1
11
if ((arr[idx] + cntflip) % 2 == 0) {
12
arr[idx]++;
13
cnt++;
14
}
15
}
16
void unfix(int idx) {
17
// means current bit is 1,so set it to 0
18
if ((arr[idx] + cntflip) % 2 != 0) {
19
arr[idx]--;
20
cnt--;
21
}
22
}
23
void flip() {
24
// cnt will flip ,if we flip all the bits
25
cnt = arr.size() - cnt;
26
cntflip++;
27
}
28
bool all() {
29
if (cnt == arr.size()) return true;
30
return false;
31
}
32
bool one() {
33
if (cnt >= 1) return true;
34
return false;
35
}
36
int count() {
37
return cnt;
38
}
39
string toString() {
40
string ans;
41
for (auto &ele : arr) {
42
if ((cntflip + ele) % 2 == 0)
43
ans.push_back('0');
44
else
45
ans.push_back('1');
46
}
47
return ans;
48
}
49
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0