One line python solution


  • 1
    J
    class Solution(object):
        def isPowerOfFour(self, num):
            return num!=0 and num&(num-1) == 0 and num|5726623061 == 5726623061
    

  • 0
    R

    Nice code! Can you explain how do you create the magic number?


  • 1
    J

    The number is 4^0+4^1+4^2...+4^n = 2^0+2^2+2^4...+2^2n. Since the integer is 32 bits, 2n need to be smaller than 31. Then just calculate this magic number ^_^.


Log in to reply
 

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