Three if statements and 4ms


  • 0
    L
    class Solution {
    public:
        bool isPowerOfFour(int num) {
            int mask = 0x55555554;  // if you do 4 + 4^2 + 4^3
            if (num == 0)
                return false;
            if (num == 1)
                return true;
            if (num & (num - 1))
                return false;
            if (num & (~mask))
                return false;
            else
                return true;
        }
    };

Log in to reply
 

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