v1
- 递归
class Solution:
def isPowerOfFour(self, n: int) -> bool:
""" 递归 """
if n <= 0:
return False
elif n == 1:
return True
elif n % 4 == 0:
return self.isPowerOfFour(n//4)
else:
return False
v2
- 迭代 一直对4取余 直到为0
Pass
v3
- 官方题解4 先判断是否为2的幂 再对3取模余1的数为4的幂
class Solution:
def isPowerOfFour(self, n: int) -> bool:
""" 题解4方法 """
return n > 0 and n & (n-1) == 0 and n % 3 == 1