Python solution-sharing


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

  • 0
    T

    It seems that loop is not allowed.


  • 0
    C

    思路,先求对数再乘方,看得到的值是否相等!

    Thinking, logarithm, then determine, equal or not?

    version2:
    class Solution(object):
        def isPowerOfThree(self, n):
            """
            :type n: int
            :rtype: bool
            """
            return n > 0 and 3 ** round(math.log(n,3)) == n

  • 0
    C

    You can see the version 2!


Log in to reply
 

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