Given an integer, write a function to determine if it is a power of three.

Follow up:
Could you do it without using any loop / recursion?

Credits:
Special thanks to @dietpepsi for adding this problem and creating all test cases.

给一个整数,写一个函数来判断此数是不是3的次方数。

类似的题目Power of Two 中,由于2的次方数的特点,用位操作很容易。而3的次方数没有显著的特点,最直接的方法就是不停地除以3,最后判断是否能整除。

follow up是否不用任何循环或递归。

解法1: 循环

解法2: 迭代

解法3:取对数

Java:

class Solution {
public boolean isPowerOfThree(int n) {
return n > 0 && Math.pow(3, Math.round(Math.log(n) / Math.log(3))) == n;
}
}  

Python:

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

Python:

class Solution(object):
def isPowerOfThree(self, n):
"""
:type n: int
:rtype: bool
"""
if n <= 0: return False
if n == 1: return True
return n % 3 == 0 and self.isPowerOfThree(n / 3) 

Python:

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

Python:

class Solution(object):
def isPowerOfThree(self, n):
return n > 0 and (math.log10(n)/math.log10(3)).is_integer()

Python:  

class Solution(object):
def __init__(self):
self.__max_log3 = int(math.log(0x7fffffff) / math.log(3))
self.__max_pow3 = 3 ** self.__max_log3 def isPowerOfThree(self, n):
"""
:type n: int
:rtype: bool
"""
return n > 0 and self.__max_pow3 % n == 0

C++:

class Solution {
public:
bool isPowerOfThree(int n) {
if(n <= 0) return false;
while(n > 1){
if(n %3 != 0) return false;
n/=3;
}
return true;
}
};  

C++:

class Solution {
public:
bool isPowerOfThree(int n) {
if (n <= 0) return false;
if (n == 1) return true;
return n % 3 == 0 && isPowerOfThree(n / 3);
}
};  

C++:

class Solution {
public:
bool isPowerOfThree(int n) {
return n > 0 && pow(3, round(log(n) / log(3))) == n;
}
};

  

  

All LeetCode Questions List 题目汇总

[LeetCode] 326. Power of Three 3的次方数的更多相关文章

  1. [LeetCode] 342. Power of Four 4的次方数

    Given an integer (signed 32 bits), write a function to check whether it is a power of 4. Example:Giv ...

  2. [LeetCode] 231. Power of Two 2的次方数

    Given an integer, write a function to determine if it is a power of two. Example 1: Input: 1 Output: ...

  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. 39. leetcode 326. Power of Three

    326. Power of Three Given an integer, write a function to determine if it is a power of three. Follo ...

  5. 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 ...

  6. 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 ...

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

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

  8. leetcode 326 Power of Three (python)

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

  9. Leetcode 326 Power of Three 数论

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

随机推荐

  1. jmeter+nmon+crontab简单的执行接口定时压测

    一.概述 临时接到任务要对系统的接口进行压测,上面的要求就是:压测,并发2000 在不熟悉系统的情况下,按目前的需求,需要做的步骤: 需要有接口脚本 需要能监控系统性能 需要能定时执行脚本 二.观察 ...

  2. 使用Xpath+多进程爬取诗词名句网的史书典籍类所有文章。update~

    上次写了爬取这个网站的程序,有一些地方不完善,而且爬取速度较慢,今天完善一下并开启多进程爬取,速度就像坐火箭.. # 需要的库 from lxml import etree import reques ...

  3. 编程小白入门分享五:Vue的自定义组件

    前言 上篇博客简单介绍了vue,本篇博客要在对vue有一定了解后,才可以比较容易理解自定义组件.想要封装好一个组件,一定要熟练掌握这三个技能,父组件 -> 子组件传值(props).子组件 -& ...

  4. Linux操作系统性能调优的方法

    http://www.cnblogs.com/L-H-R-X-hehe/p/3963442.html Linux是一套免费使用和自由传播的类Unix操作系统,Linux不同的发行版本和不同的内核对各项 ...

  5. python 格式化输出%s %f %d

    格式说明由%和格式字符组成,如%f,它的作用是将数据按照指定的格式输出.格式说明是由“%”字符开始的. 1.整型输出%d print 'my age is %d'% (26) 说明:%d相当于是一个占 ...

  6. [TJOI2015]概率论——期望&&母函数

    题意 求一个含有 $n$ 个结点的有序二叉树的叶子节点的期望个数.($n \leq 10^9$) 分析 一堆推导..... 得 $ans = \frac{n(n+1)}{2(2n-1)}$ #incl ...

  7. mysql udf提权

    参考文章:https://blog.csdn.net/q1352483315/article/details/98483668

  8. AnsiString

    原文链接:https://blog.csdn.net/Li_Ning_/article/details/82981092 /* * 编号:Number 1 * 函数:substring * 说明:截取 ...

  9. Linux下搭建iSCSI共享存储的方法 Linux-IO Target 方式CentOS7-1810下实现

    iSCSI(internet SCSI)技术由IBM公司研究开发,是一个供硬件设备使用的.可以在IP协议的上层运行的SCSI指令集,这种指令集合可以实现在IP网络上运行SCSI协议,使其能够在诸如高速 ...

  10. 洛谷 P2422 良好的感觉 题解

    P2422 良好的感觉 题目描述 kkk做了一个人体感觉分析器.每一天,人都有一个感受值Ai,Ai越大,表示人感觉越舒适.在一段时间[i, j]内,人的舒适程度定义为[i, j]中最不舒服的那一天的感 ...