Leetcode # 326. Power of Three
Problem
https://leetcode.com/problems/power-of-three
Solution
⚠️ 注意 if 順序
if n == 1: return True
if n < 1 or n % 3 != 0: return False
if n < 1 or n % 3 != 0: return False
if n == 1: return True
因為 1 % 3 == 1 != 0 會導致if n == 1
執行不到
Code
Time Complexity: O(log(n, 3)) = O(log(n))
Space Complexity: O(log(n, 3)) = O(log(n))
(The input and output generally do not count towards the space complexity.)
class Solution: def isPowerOfThree(self, n: int) -> bool: if n == 1: return True if n < 1 or n % 3 != 0: return False return self.isPowerOfThree(n // 3)
Last Updated on 2025/08/13 by A1go