Skip to content

Latest commit

 

History

History
28 lines (25 loc) · 653 Bytes

326.md

File metadata and controls

28 lines (25 loc) · 653 Bytes

A summary.

Solution 1

class Solution(object):
    def isPowerOfThree(self, n):
        """
        :type n: int
        :rtype: bool
        """
        if n < 1: return False
        
        while n != 1:
            if n % 3 != 0:
                return False
            n //= 3
        
        return True

Time complexity: O(log(N)) Space complexity: O(1)

Solution 2 Math

def isPowerOfThree(n):
    # 3^20 > maxInt32
    return n > 0 and 3**19 % n == 0