1780. Check if Number is a Sum of Powers of Three
Read the full problem statement on LeetCode.
Difficulty: medium Acceptance: 79% Topics: Math
View full problem on LeetCode Reading material
Reference solution (spoiler · python)
# Time: O(logn)
# Space: O(1)
class Solution(object):
def checkPowersOfThree(self, n):
"""
:type n: int
:rtype: bool
"""
while n > 0:
if n%3 == 2:
return False
n //= 3
return True
Solution from kamyu104/LeetCode-Solutions · MIT
Similar questions