03_3的幂
2019-11-04 本文已影响0人
butters001
# 使用了循环 不符合要求
class Solution(object):
def isPowerOfThree(self, n):
"""
:type n: int
:rtype: bool
"""
while True:
if n % 3 == 0:
if n == 3:
return True
n /= 3
else:
return False
class Solution2(object):
def isPowerOfThree(self, n):
"""
:type n: int
:rtype: bool
"""
return n > 0 and 3 ** 19 % n == 0
class Solution3(object):
def isPowerOfThree(self, n):
"""
:type n: int
:rtype: bool
"""
if n < 1:
return False
while n % 3 == 0:
n /= 3
return n == 1
n = 27
s = Solution()
print(s.isPowerOfThree(n))