LeetCode 326 – Python 3 (Week 06 – 09)

Solution 1

class Solution:
    def isPowerOfThree(self, n: int) -> bool:
        maxint = 2 ** 31 - 1

        return n > 0 and 3 ** int(math.log(maxint,3)) % n == 0
    

Time complexity is O(1). Space complexity is O(1).

Leave a comment

Design a site like this with WordPress.com
Get started