Design Bitset
expert · 1095 · lc medium +32 · 32.6% accepted · 612 likes · top 10%
Description
A Bitset is a data structure that stores bits compactly.
Implement the Bitset class:
- Bitset(int size) Initializes the Bitset with size bits, all set to 0.
- void fix(int idx) Sets the bit at index idx to 1. If already 1, no change.
- void unfix(int idx) Sets the bit at index idx to 0. If already 0, no change.
- void flip() Inverts every bit: all 0s become 1s and vice versa.
- boolean all() Returns true if every bit is 1, otherwise false.
- boolean one() Returns true if at least one bit is 1, otherwise false.
- int count() Returns the total number of bits currently set to 1.
- String toString() Returns the current bit sequence as a string where the character at position i reflects the ith bit's value.
Example 1:
Example 2:
Code