本来想通过跟power of four一模一样的方法来解题。http://blog.youkuaiyun.com/howtogetout/article/details/51615930
但是发现了int(math.log(243, 3))等于4,而不是等于5。243的平方等都会出现类似问题。所以修改了一下。
class Solution(object):
def isPowerOfThree(self, n):
"""
:type n: int
:rtype: bool
"""
if n <= 0:
return False
if 3 ** int(round(math.log(n, 3))) == n:
return True
else:
return False
采用了round取整数部分来解决问题。