Given an integer, write a function to determine if it is a power of three.
Follow up:
Could you do it without using any loop / recursion?
Python:
迭代:
class Solution(object):
def isPowerOfThree(self, n):
"""
:type n: int
:rtype: bool
"""
if n == 1:
return True
elif n == 0 or n % 3 != 0:
return False
return self.isPowerOfThree(n/3)
import math
class Solution(object):
def isPowerOfThree(self, n):
"""
:type n: int
:rtype: bool
"""
if n > 0 and 3 ** round(math.log(n, 3)) == n:
return True
else:
return False