As a signed 32 int, there are only 16 possibilities.


  • 0

    So I check for them all:

    class Solution(object):
        def isPowerOfFour(self, num):
            """
            :type num: int
            :rtype: bool
            """
            
            # for a signed 32 bit int, there is really only 16 possibilities to be a power of 4
            powerof4s = [1, 4, 16, 64, 256, 1024, 4096, 16384, 65536, 262144, 
            1048576, 4194304, 16777216, 67108864, 268435456, 1073741824]
            
            return num in powerof4s
    

    There is a loop but still O(1) running time :) .


Log in to reply
 

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