mycode

class Solution(object):
    def isPowerOfThree(self, n):
        """
        :type n: int
        :rtype: bool
        """
        while n > 2:
            if n%3 != 0 :
                return False
            n = n // 3
            print(n)
        return n == 1

 

参考

class Solution(object):
    def isPowerOfThree(self, n):
        """
        :type n: int
        :rtype: bool
        """
        if n <= 0:
            return False
        while n%3 == 0:
            n = n/3
        return n==1