Skip to content

Commit 800571a

Browse files
committed
add 2166
1 parent 79d218a commit 800571a

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

2166-design-bitset.py

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
"""
2+
2166. Design Bitset
3+
4+
Submitted: February 28, 2025
5+
6+
Runtime: 1206 ms (beats 21.92%)
7+
Memory: 49.69 MB (beats 96.58%)
8+
"""
9+
10+
class Bitset:
11+
12+
def __init__(self, size: int):
13+
self.size = size
14+
self.data = 0
15+
16+
def fix(self, idx: int) -> None:
17+
self.data |= (1 << idx)
18+
19+
def unfix(self, idx: int) -> None:
20+
self.data &= ~(1 << idx)
21+
22+
def flip(self) -> None:
23+
self.data = (~self.data & (1 << self.size) - 1)
24+
25+
def all(self) -> bool:
26+
return self.data == (1 << self.size) - 1
27+
28+
def one(self) -> bool:
29+
return bool(self.data)
30+
31+
def count(self) -> int:
32+
return self.data.bit_count()
33+
34+
def toString(self) -> str:
35+
return bin(self.data)[2:].zfill(self.size)[::-1]
36+
37+
# Your Bitset object will be instantiated and called as such:
38+
# obj = Bitset(size)
39+
# obj.fix(idx)
40+
# obj.unfix(idx)
41+
# obj.flip()
42+
# param_4 = obj.all()
43+
# param_5 = obj.one()
44+
# param_6 = obj.count()
45+
# param_7 = obj.toString()

0 commit comments

Comments
 (0)