Python 1-line solution easy explanation


  • 1
    def isPowerOfFour(self, num):
        return (num.bit_length()%2==1 and num & (num-1) == 0)
    

    Two conditions:

    First, odd length

    Second, 1 & 0, 4 & 3, 16 & 15 are all 0


Log in to reply
 

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