-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathDesign Bitset.cpp
51 lines (51 loc) · 1.03 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
class Bitset {
public:
vector<int>arr;
int cnt,cntflip;
Bitset(int size) {
arr.resize(size,0);
cnt=0,cntflip=0;
}
void fix(int idx) {
// means current bit is 0 ,so set it to 1
if((arr[idx]+cntflip)%2==0){
arr[idx]++;
cnt++;
}
}
void unfix(int idx) {
// means current bit is 1,so set it to 0
if((arr[idx]+cntflip)%2!=0){
arr[idx]--;
cnt--;
}
}
void flip() {
// cnt will flip ,if we flip all the bits
cnt=arr.size()-cnt;
cntflip++;
}
bool all() {
if(cnt==arr.size())
return true;
return false;
}
bool one() {
if(cnt>=1)
return true;
return false;
}
int count() {
return cnt;
}
string toString() {
string ans;
for(auto &ele :arr){
if((cntflip+ele)%2==0)
ans.push_back('0');
else
ans.push_back('1');
}
return ans;
}
};