idea from power of 2 without loop

  • 0

    if num is power of 4, then it's square of a power of 2, so take square root, if it's power of 2, then it's true

    def isPowerOfFour(self, num):
            :type num: int
            :rtype: bool
            if num<1: return False  #exclude negative numbers
            if num**(1/2)%1!=0: return False #check whether is a qaure number
            num=int(num**(1/2)) #take the square root
            return not num&(num-1)

Log in to reply

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