One line Math solution python code


  • 1

    The main idea is checking whether log(n, 3) is an integer.

    class Solution(object):
        def isPowerOfThree(self, n):
            return n > 0 and abs(math.log(n, 3) - round(math.log(n, 3))) < 1e-10

  • 0
    D

    Thanks for this. I went the same route but was having trouble because I was using sys.float_info.epsilon, which was making certain test cases fail.


  • 0

    Glad to help you :)


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.