2166. 设计位集
为保证权益,题目请参考 2166. 设计位集(From LeetCode).
解决方案1
CPP
C++
#include <memory>
#include <string>
using namespace std;
class Bitset {
private:
int size = 0;
unique_ptr<int[]> ptr;
int oneNum = 0;
int zeroNum = 0;
int fliped = 0;
public:
Bitset(int size) {
this->size = size;
this->ptr = make_unique<int[]>(size);
this->oneNum = 0;
this->zeroNum = size;
this->fliped = 0;
}
void fix(int idx) {
if (this->fliped == 1) {
if (this->ptr[idx] == 1) {
oneNum -= 1;
zeroNum += 1;
}
this->ptr[idx] = 0;
} else {
if (this->ptr[idx] == 0) {
oneNum += 1;
zeroNum -= 1;
}
this->ptr[idx] = 1;
}
}
void unfix(int idx) {
if (this->fliped == 1) {
if (this->ptr[idx] == 0) {
oneNum += 1;
zeroNum -= 1;
}
this->ptr[idx] = 1;
} else {
if (this->ptr[idx] == 1) {
oneNum -= 1;
zeroNum += 1;
}
this->ptr[idx] = 0;
}
}
void flip() { this->fliped = 1 - this->fliped; }
bool all() {
if (this->fliped == 1) {
return this->zeroNum == this->size;
} else {
return this->oneNum == this->size;
}
}
bool one() {
if (this->fliped == 1) {
return this->zeroNum >= 1;
} else {
return this->oneNum >= 1;
}
}
int count() {
if (this->fliped == 1) {
return this->zeroNum;
} else {
return this->oneNum;
}
}
string toString() {
string ans = "";
for (int i = 0; i < size; i++) {
if ((this->ptr[i] == 1 && this->fliped == 0) ||
(this->ptr[i] == 0 && this->fliped == 1)) {
ans.push_back('1');
} else {
ans.push_back('0');
}
}
return ans;
}
};
/**
* Your Bitset object will be instantiated and called as such:
* Bitset* obj = new Bitset(size);
* obj->fix(idx);
* obj->unfix(idx);
* obj->flip();
* bool param_4 = obj->all();
* bool param_5 = obj->one();
* int param_6 = obj->count();
* string param_7 = obj->toString();
*/
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
99
100
101
102
103
104
105
106
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
99
100
101
102
103
104
105
106