Skip to content

Practice Design Bitset

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#2166
LeetCode Problem

Design Bitset

Problem Statement

Bitset(size) starts with zero bits. fix(idx) sets one, unfix(idx) sets zero, and flip() reverses every bit. all() checks whether all bits are one; one() checks for any one; count() returns the number of ones; toString() emits bits in index order. Repeating fix or unfix is idempotent. Practice approach: Keep stored bits, a lazy flip flag, and the logical one count. A global flip toggles the flag and replaces count with size - count. Local updates adjust count only when the logical bit changes. All operations except output take O(1); toString requires O(size).

Source: Design Bitset on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
["Bitset","fix","fix","flip","toString","count","unfix","one","all"]
[[3],[1],[1],[],[],[],[0],[],[]]
Output
[null,null,null,null,"101",2,null,true,false]
Explanation

Fixing bit 1 twice leaves 010; flipping gives 101, and unfixing bit 0 gives 001.

Constraints

1 <= size <= 10^5
0 <= idx < size
At most 10^5 total calls.
At most five toString calls.
At least one query method is called.

Related LeetCode Problems

Practice more problems similar to Design Bitset.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.