• 136-326. 3的幂数


    给定一个整数,写一个函数来判断它是否是 3 的幂次方。如果是,返回 true ;否则,返回 false 。(第一个我写的)
    
    class Solution(object):
        def isPowerOfThree1(self, n):
            """我的偷懒解法
            :type n: int
            :rtype: bool
            """
            if n <= 0:
                return False
            return str(math.log(n, 3)).endswith(".0")
    
        def isPowerOfThree2(self, n):
            """
            :type n: int
            :rtype: bool
            """
            if n <= 0:
                return False
    
            while n % 3 == 0:
                n //= 3
            return n == 1
    
        def isPowerOfThree(self, n):
            """
            :type n: int
            :rtype: bool
            """
            import math
            if n <= 0:
                return False
            res = math.log10(n)/math.log10(3)
            return res == int(res)
    
    
    if __name__ == '__main__':
        import math
        s = Solution()
        n = 45
        print(s.isPowerOfThree(n))
    
  • 相关阅读:
    小批量随机梯度下降
    查询文档
    自动求梯度
    数据操作
    Hadoop 入门
    Matplotlib 二维图像绘制方法
    Pandas 数据处理基础
    NumPy 数值计算基础课程
    关于 Shell 脚本
    语法分析
  • 原文地址:https://www.cnblogs.com/liuzhanghao/p/14261192.html
Copyright © 2020-2023  润新知