LeetCode 2166: Design Bitset Solution

Master LeetCode problem 2166 (Design Bitset), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

2166. Design Bitset

Problem Explanation

Explanation

To implement the Bitset class, we can use an array of boolean values to represent the bits. We will initialize the array with the specified size, and each index will correspond to a bit. The fix, unfix, and flip methods will update the corresponding bits accordingly. The all and one methods will check if all bits are 1 or if at least one bit is 1, respectively. The count method will count the number of bits with value 1. The toString method will return a string representation of the current composition of the Bitset.

Time Complexity

  • Constructor: O(size)
  • fix, unfix, flip: O(1)
  • all, one, count: O(size)
  • toString: O(size) or O(1) if we maintain a separate string representation

Space Complexity

O(size)

Solution Code

class Bitset {
    boolean[] bits;

    public Bitset(int size) {
        bits = new boolean[size];
    }

    public void fix(int idx) {
        if (idx >= 0 && idx < bits.length) {
            bits[idx] = true;
        }
    }

    public void unfix(int idx) {
        if (idx >= 0 && idx < bits.length) {
            bits[idx] = false;
        }
    }

    public void flip() {
        for (int i = 0; i < bits.length; i++) {
            bits[i] = !bits[i];
        }
    }

    public boolean all() {
        for (boolean bit : bits) {
            if (!bit) {
                return false;
            }
        }
        return true;
    }

    public boolean one() {
        for (boolean bit : bits) {
            if (bit) {
                return true;
            }
        }
        return false;
    }

    public int count() {
        int count = 0;
        for (boolean bit : bits) {
            if (bit) {
                count++;
            }
        }
        return count;
    }

    public String toString() {
        StringBuilder sb = new StringBuilder();
        for (boolean bit : bits) {
            sb.append(bit ? '1' : '0');
        }
        return sb.toString();
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 2166 (Design Bitset)?

This page provides optimized solutions for LeetCode problem 2166 (Design Bitset) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 2166 (Design Bitset)?

The time complexity for LeetCode 2166 (Design Bitset) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 2166 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 2166 in Java, C++, or Python.

Back to LeetCode Solutions