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
| class Bitset:
def __init__(self, size: int):
self.size = size
self.ones = set() # To store the indexes that have a value of 1
self.zero = set(range(size)) # To store the indexes that have a value of 0
def fix(self, idx: int) -> None:
self.ones.add(idx)
if idx in self.zero: # Check if idx is in zero before attempting to remove
self.zero.remove(idx)
def unfix(self, idx: int) -> None:
if idx in self.ones: # Check if idx is in ones before attempting to remove
self.ones.remove(idx)
self.zero.add(idx)
def flip(self) -> None:
self.ones, self.zero = self.zero, self.ones
def all(self) -> bool:
return len(self.ones) == self.size
def one(self) -> bool:
return len(self.ones) >= 1
def count(self) -> int:
return len(self.ones)
def toString(self) -> str:
sb = []
for i in range(self.size):
if i in self.ones:
sb.append("1")
elif i in self.zero:
sb.append("0")
return ''.join(sb)
|