zoukankan      html  css  js  c++  java
  • leetcode_326. 3的幂

    给定一个整数,写一个函数来判断它是否是 3 的幂次方。如果是,返回 true ;否则,返回 false 。
    
    整数 n 是 3 的幂次方需满足:存在整数 x 使得 n == 3x
    
     
    
    示例 1:
    
    输入:n = 27
    输出:true
    示例 2:
    
    输入:n = 0
    输出:false
    示例 3:
    
    输入:n = 9
    输出:true
    示例 4:
    
    输入:n = 45
    输出:false
     
    
    提示:
    
    -231 <= n <= 231 - 1
     
    
    来源:力扣(LeetCode)
    链接:https://leetcode-cn.com/problems/power-of-three
    著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
    
    class Solution:
        def isPowerOfThree(self, n: int) -> bool:
            t=1
            while(t<n):
                t*=3
            return not t>n
    
    class Solution:
        def isPowerOfThree(self, n: int) -> bool:
            if n==0:
                return False
            while(n%3==0):
                n/=3
            return n==1
    
  • 相关阅读:
    晶振及COMS电路
    笔记16 C# typeof() & GetType()
    笔记15 修饰符
    笔记14 数据库编程技术
    C#基础知识
    C#连接数据库
    笔记13 winform
    笔记12 export to excel (NPOI)
    笔记11 export to excel
    笔记10
  • 原文地址:https://www.cnblogs.com/hqzxwm/p/14131673.html
Copyright © 2011-2022 走看看