问题描述:

给定一个整数,写一个函数来判断它是否是 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. Installing Jenkins as a Windows service

    Install Jenkins as a Windows service NOTE: if you installed Jenkins using the windows installer, you ...

  2. SSM到Spring Boot从零开发校园商铺平台

    项目目的 特别 由于准备春招,所以希望各位看客方便的话,能去github上面帮我Star一下项目 https://github.com/Draymonders/Campus-Shop emmm, 已经 ...

  3. P2519 [HAOI2011]problem a

    思路 神仙思路,就差一步就能想出来了... 看到第i个人给出的条件,发现有\(a_i\)个大于,\(b_i\)个小于并不好处理 考虑把条件转化成第i个人对应的排名处理,设第i个人的排名为\(a_i+1 ...

  4. Luncene学习二《搜索索引》

    搜索索引的流程 第一步:创建一个Directory对象,也就是索引库存放的位置 第二步:创建一个IndexReader对象,需要指定Directory对象 第三步:创建一个indexsearcher对 ...

  5. using Redis in .net core

    Using Redis Cache in .net Core Distributed Cache using Redis and ASP.NET Core ASP.NET Core Data Prot ...

  6. Ambari配置Hive,Hive的使用

    mysql安装,hive环境的搭建 ambari部署hadoop 博客大牛:董的博客 ambari使用 ambari官方文档 hadoop 2.0 详细配置教程 使用Ambari快速部署Hadoop大 ...

  7. wow.js

    一.首先说明一下怎么使用这个插件: 1.wow.js依赖于animate.css,首先在头部引用animate.css或者animate.min.css. <link rel="sty ...

  8. Mysql 函数使用记录(三)——UNIX_TIMESTAMP() 、UNIX_TIMESTAMP(date)

    参考资料:https://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html#function_unix-timestamp UN ...

  9. Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) E. Cards Sorting 树状数组

    E. Cards Sorting time limit per test 1 second memory limit per test 256 megabytes input standard inp ...

  10. Spring重要注解@ControllerAdvice

    @ControllerAdvice是一个@Component,用于定义@ExceptionHandler,@InitBinder和@ModelAttribute方法,适用于所有使用@RequestMa ...