C++ solution using bitset<32>

  • 0

    Any decimal number that is a power of two only has one "1" in binary form.

    bool isPowerOfTwo(int n) {
            if (n < 1) return false;
            bitset<32> num(n);
            return num.count() == 1;

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.