对数计算法
执行用时:76 ms, 在所有 Python3 提交中击败了94.26%的用户
内存消耗:14.7 MB, 在所有 Python3提交中击败了85.61%的用户
class Solution:
def isPowerOfThree(self, n: int) -> bool:
if n<=0:
return False
e=log10(n)/log10(3)
return floor(e)==e
循环迭代法
class Solution:
def isPowerOfThree(self, n: int) -> bool:
if n<=0:
return False
while n>1:
if n%3==0:
n=int(n/3)
else:
return False
return True
投机取巧法
整数限制法
return n>0 and (3**19)%n==0