Skip to content

326. Power of Three

https://leetcode.com/problems/power-of-three/

js
/**
 * @param {number} n
 * @return {boolean}
 */
var isPowerOfThree = function(n) {
  if (n <= 0) {
    return false;
  }
  if (n === 1) {
    return true;
  }
  var r = n / 3;
  return r >> 0 === r ? isPowerOfThree(r) : false;
}
py
class Solution(object):

    def isPowerOfThree(self, n):
        """
        :type n: int
        :rtype: bool
        """
        if n <= 0:
            return False
        if n == 1:
            return True
        return self.isPowerOfThree(n // 3) if n % 3 == 0 else False