forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
design-bitset.cpp
62 lines (52 loc) · 1.13 KB
/
design-bitset.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// Time: ctor: O(n)
// fix: O(1)
// unfix: O(1)
// flip: O(1)
// all: O(1)
// one: O(1)
// count: O(1)
// toString: O(n)
// Space: O(n)
// design
class Bitset {
public:
Bitset(int size)
: lookup_(size) {
}
void fix(int idx) {
if (lookup_[idx] == flip_) {
lookup_[idx] = !lookup_[idx];
++cnt_;
}
}
void unfix(int idx) {
if (lookup_[idx] != flip_) {
lookup_[idx] = !lookup_[idx];
--cnt_;
}
}
void flip() {
flip_ = !flip_;
cnt_ = size(lookup_) - cnt_;
}
bool all() {
return cnt_ == size(lookup_);
}
bool one() {
return cnt_ >= 1;
}
int count() {
return cnt_;
}
string toString() {
string result(size(lookup_), '0');
for (int i = 0; i < size(result); ++i) {
result[i] = (lookup_[i] != flip_) ? '1' : '0';
}
return result;
}
private:
vector<bool> lookup_;
bool flip_ = false;
int cnt_ = 0;
};