本来想通过跟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取整数部分来解决问题。
本文介绍了一种判断整数是否为3的幂次方的有效方法。通过修正int(math.log(243,3))计算不准确的问题,采用round函数取整数部分确保了判断的准确性。
810

被折叠的 条评论
为什么被折叠?



