LeetCode - Power of Three(Python)


Problem


3の平方数かどうかを判断します.
Site

Solution


1) Iterative

class Solution:
    def isPowerOfThree(self, n: int) -> bool:
        while n > 1:           
            if n % 3 != 0:
                return False  
            else:
                n //= 3
        
        return True

2) Recursive

class Solution:
    def isPowerOfThree(self, n: int) -> bool:
        if n == 1:
            return True

        elif n <= 0:
            return False

        if n % 3 != 0:
            return False

        else:
            return self.isPowerOfThree(n // 3)