342. Power of Four

# Easy

Two methods: 第二种方法是技巧型,很难想出来

  1. loop

  2. bit manipulate according to #Power of Two. If num & (num-1) = 0, then num is power of two. If num is power of two and (num-1)%3 = 0, then num is power of four.

class Solution:
    def isPowerOfFour(self, num: int) -> bool:
        if num < 0:
            return False
        if num & (num-1) != 0:
            return False
        if (num-1)%3 != 0:
            return False
        return True
        

Last updated