Input:
Solution.init(10)Solution.set(3,1)Solution.set(5,1)print(Solution.get(3))Output:
1Explanation: The value at index 3 was set to 1, and the value at index 5 was also set to 1.
classSolution:
classBitArray:
def __init__(self):
self.bits: List[int] = []
self.size: int =0definit(self, size: int) ->None:
self.size = size
self.bits = [0] * ((size +31) //32)
defset(self, i: int, val: int) ->None:
if i >= self.size or i <0:
raiseIndexError("Index out of bounds")
array_index = i //32 bit_index = i %32if val ==1:
self.bits[array_index] |= (1<< bit_index)
else:
self.bits[array_index] &=~(1<< bit_index)
defget(self, i: int) -> int:
if i >= self.size or i <0:
raiseIndexError("Index out of bounds")
array_index = i //32 bit_index = i %32return (self.bits[array_index] >> bit_index) &1