Posted on

Description

Submission

class Solution {
public:
    bool isPowerOfTwo(int n) {
        if(n < 0) return false;
        int count = 0;
        for(int i = 1; i <= 32; i++, n >>= 1) {
            if(n & 1) count++;
            if(count > 1) return false;
        }
        return count == 1;
    }
};

Submission II

class Solution {
public:
    bool isPowerOfTwo(int n) {
        if(n <= 0) return false;
        return !(n & (n - 1));
    }
};

Leave a Reply

Your email address will not be published. Required fields are marked *