给出一个整数,写一个函数来确定这个数是不是3的一个幂。
后续挑战:
你能不使用循环或者递归完成本题吗?
class Solution(object):
def isPowerOfThree(self, n):
"""
:type n: int
:rtype: bool
"""
while n>0:
if n%3==0:
n/=3
else:
break
return True if n==1 else False
后续挑战
class Solution(object):
def isPowerOfThree(self, n):
"""
:type n: int
:rtype: bool
"""
return n>0 and 1162261467%n==0 #3^19=1162261467是小于2^31最大的3的倍数
另外有个问题
class Solution(object):
def isPowerOfThree(self, n):
"""
:type n: int
:rtype: bool
"""
if n<=0:
return False
return True if pow(3,int(math.log(n,3)))==n else False #这pow(3,int(math.log(n,3)))中243不通过,为何
