-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2166.cpp
84 lines (73 loc) · 1.6 KB
/
2166.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include<bits/stdc++.h>
using namespace std;
class Bitset {
private:
int size;
vector<int> bits;
int flipped = 0;
int count1 = 0;
public:
Bitset(int size) {
bits.resize(size);
this->size = size;
}
void fix(int idx) {
if(flipped && bits[idx] == 1){
bits[idx] = 0;
count1++;
}else if(!flipped && bits[idx] == 0){
bits[idx] = 1;
count1++;
}
}
void unfix(int idx) {
if(flipped && bits[idx] == 0){
bits[idx] = 1;
count1--;
}else if(!flipped && bits[idx] == 1){
bits[idx] = 0;
count1--;
}
}
void flip() {
this->flipped += 1;
this->flipped %= 2;
count1 = size - count1;
}
bool all() {
if(count1 == size)
return true;
else return false;
}
bool one() {
if(count1 > 0)
return true;
else
return false;
}
int count() {
return count1;
}
string toString() {
string res;
for(int i = 0; i < size; i++){
if((bits[i] ^ flipped) == 0){
res += '0';
}else{
res += '1';
}
}
return res;
}
};
/**
* 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();
*/