问题描述:

给定一个整数,写一个函数来判断它是否是 3 的幂次方。

示例 1:

输入: 27
输出: true

示例 2:

输入: 0
输出: false

示例 3:

输入: 9
输出: true

示例 4:

输入: 45
输出: false

方法:取243时,会出错。log(243,3) == 4.9999... 用round 四舍五入。(时间太长)

 import math
class Solution: def isPowerOfThree(self, n):
"""
:type n: int
:rtype: bool
"""
if n > 0:
return pow(3,round(math.log(n,3))) == n
else:
return False

官方:3^19=1162261467是小于2^31最大的3的倍数

 class Solution:
def isPowerOfThree(self, n):
"""
:type n: int
:rtype: bool
"""
maxThreeInt = 3**19 return n > 0 and maxThreeInt % n == 0

循环:

 import math
class Solution: def isPowerOfThree(self, n):
"""
:type n: int
:rtype: bool
"""
if n > 0:
if n == 1:
return True
else:
k = 0
while k == 0 :
n = n / 3.0
k = n % 3
if n == 1:
return True
else:
return False
else:
return False

2018-09-25 21:03:03

LeetCode--326--3的幂的更多相关文章

  1. Java实现 LeetCode 326 3的幂

    326. 3的幂 给定一个整数,写一个函数来判断它是否是 3 的幂次方. 示例 1: 输入: 27 输出: true 示例 2: 输入: 0 输出: false 示例 3: 输入: 9 输出: tru ...

  2. Leetcode 326.3的幂 By Python

    给定一个整数,写一个函数来判断它是否是 3 的幂次方. 示例 1: 输入: 27 输出: true 示例 2: 输入: 0 输出: false 示例 3: 输入: 9 输出: true 示例 4: 输 ...

  3. leetcode 326. Power of Three(不用循环或递归)

    leetcode 326. Power of Three(不用循环或递归) Given an integer, write a function to determine if it is a pow ...

  4. LeetCode 231.2的幂

    LeetCode 231.2的幂 题目: 给定一个整数,编写一个函数来判断它是否是 2 的幂次方. 算法: 若一个数是2的幂次的话定会有n & (n - 1) == 0这个关系成立 所以直接用 ...

  5. 不使用循环或递归判断一个数是否为3的幂(leetcode 326)

    326. Power of ThreeGiven an integer, write a function to determine if it is a power of three. Follow ...

  6. leetcode刷题笔记326 3的幂

    题目描述: 给出一个整数,写一个函数来确定这个数是不是3的一个幂. 后续挑战:你能不使用循环或者递归完成本题吗? 题目分析: 既然不使用循环或者递归,那我可要抖机灵了 如果某个数n为3的幂 ,则k=l ...

  7. LeetCode 326 Power of Three(3的幂)(递归、Log函数)

    翻译 给定一个整型数,写一个函数决定它是否是3的幂(翻译可能不太合适-- 跟进: 你能否够不用不论什么循环或递归来完毕. 原文 Given an integer, write a function t ...

  8. LeetCode 326 Power of Three

    Problem: Given an integer, write a function to determine if it is a power of three. Could you do it ...

  9. Leetcode 326 Power of Three 数论

    判断一个数是否是3的n次幂 这里我用了一点巧,所有的int范围的3的n次幂是int范围最大的3的n次幂数(即3^((int)log3(MAXINT)) =  1162261467)的约数 这种方法是我 ...

  10. Java [Leetcode 326]Power of Three

    题目描述: Given an integer, write a function to determine if it is a power of three. Follow up:Could you ...

随机推荐

  1. 使用Jenkins构建、部署spring boot项目

    一.环境搭建 本次实验的环境为Ubuntu 16.04,Jenkins 2.8.3 1.安装ssh sudo apt-get update # 更新软件源 sudo apt-get install o ...

  2. Jenkins 总结

    步骤: 1,安装Jenkins 2,运行Jenkins: java -jar jenkins.war --httpPort=8888 httpPort指的就是Jenkins所使用的http端口,这里指 ...

  3. [WARNING]: Could not match supplied host pattern, ignoring: servers

    Centos7.5 ansible执行命令报错 问题: [root@m01 ~]# ansible servers -a "hostname" [WARNING]: provide ...

  4. FireMonkey 源码学习(3)

    五.TTextLayoutNG 在FMX.TextLayout.GPU.pas文件中,实现了几个基础功能,其中: (1)渲染单元 在TextLayout中,每一批同字体和颜色的1~n个字符,组成一个最 ...

  5. 【Spring Security】五、自定义过滤器

    在之前的几篇security教程中,资源和所对应的权限都是在xml中进行配置的,也就在http标签中配置intercept-url,试想要是配置的对象不多,那还好,但是平常实际开发中都往往是非常多的资 ...

  6. P3232 [HNOI2013]游走

    吐槽 傻了傻了,对着题解改了好长时间最后发现是自己忘了调用高斯消元了... 思路 期望题,分配编号,显然编号大的分给贡献次数小的,所以需要知道每个边被经过次数的期望 然后边被经过的次数的期望就是连接的 ...

  7. (转)Applications of Reinforcement Learning in Real World

    Applications of Reinforcement Learning in Real World 2018-08-05 18:58:04 This blog is copied from: h ...

  8. Python 安装与环境变量配置

    一.软件下载 Python安装包下载地址:https://www.python.org/ 二.安装过程(略) 三.环境变量配置: 方法一:使用cmd命令添加path环境变量 在cmd下输入: path ...

  9. linux 基本命令2(12月27日笔记)

    1.ifconfig 作用:用于操作网卡相关的指令 简单语法:#ifconfig      (获取网卡信息)   2.reboot 作用:重新启动计算机 语法1:#reboot             ...

  10. JavaScript中 call和apply

    call()方法和apply()方法的作用相同,他们的区别在于接收参数的方式不同. 对于call(),第一个参数是this值没有变化,变化的是其余参数都直接传递给函数.(在使用call()方法时,传递 ...