Simple Python solution


  • 2
    W
    def isPowerOfFour(self, num):
        """
        :type num: int
        :rtype: bool
        """
        while num > 0 and num & 3 == 0:
            num >>= 2
        return num == 1
    

    Explanation:
    1 is a power of 4 (4^0). Any power of 4 has 1 followed by an even number of 0s in its binary representation, so 3 (0b11) it can never be anded with 3 and yield anything, unless it's reached the end, in which case we check if it's equal to 1.


Log in to reply
 

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