给定一个整数 (32位有符整数型),请写出一个函数来检验它是否是4的幂。
示例:
当 num = 16 时 ,返回 true 。 当 num = 5时,返回 false。
问题进阶:你能不使用循环/递归来解决这个问题吗?
python第一版
class Solution(object):
def isPowerOfFour(self, num):
"""
:type num: int
:rtype: bool
"""
while num>0:
if num%4==0:
num/=4
else:
break
return True if num==1 else False
python进阶版
class Solution(object):
def isPowerOfFour(self, num):
"""
:type num: int
:rtype: bool
"""
if num<=0:
return False
return True if pow(4,int(math.log(num,4)))==num else False #使用log函数实现,记住加int