题目:
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?

思路:
判断一个数是否是3的幂,但是不建议用循环和递归。想到了用log函数,因为如果n是3的幂,那么log3(n)肯定是整数,将其result设成double型的,与转换成int型后的result对比,若相等则为3的幂;否则 不是3的幂。但是因为没有3的底,所以要用换底公式将底换成10.

代码:

class Solution {
public:bool isPowerOfThree(int n) {double result=log10(n)/log10(3);//换底return (result-int(result))==0?true:false;}
};

**注:
cmath中,log为以e为底,log10是以10为底,其它的都要用换底公式。**

LeetCode 326. Power of Three相关推荐

  1. LeetCode:326. Power of Three

    2019独角兽企业重金招聘Python工程师标准>>> Given an integer, write a function to determine if it is a powe ...

  2. [leetcode]326. Power of Three(c语言)

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

  3. LeetCode 326. Power of Three (算法,换底公式)

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

  4. leetcode 326. 3的幂(Power of Three)

    目录 题目描述: 示例 1: 示例 2: 示例 3: 示例 4: 进阶: 解法: 题目描述: 给定一个整数,写一个函数来判断它是否是 3 的幂次方. 示例 1: 输入: 27输出: true 示例 2 ...

  5. [LeetCode][JavaScript]Power of Three

    Power of Three Given an integer, write a function to determine if it is a power of three. Follow up: ...

  6. [leetcode] 342. Power of Four

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

  7. leetcode 342. Power of Four

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

  8. leetcode 231. Power of Two

    Given an integer, write a function to determine if it is a power of two. class Solution(object):def ...

  9. LeetCode - 231. Power of Two

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

最新文章

  1. WPF   --- xmln 两个重要的命名空间
  2. 第二章:深入C#数据类型
  3. 鸿蒙正式推送时间,鸿蒙系统现已正式推送,更新名单也随之出炉,包括荣耀手机!...
  4. C#遍历一个文件夹下的所有可执行文件
  5. vue如何获取年月日_好程序员web前端教程分享Vue相关面试题
  6. vue路由懒加载_优化vue项目的首屏加载速度
  7. linux 分区个数限制,Linux分区个数限制[转载]
  8. linux中怎么创建管道文件,Linux  管道文件
  9. python的dropna 和notna的性能_python数据分析学习(7)数据清洗与准备(1)
  10. 蓝桥杯 PREV-32 历届试题 分糖果
  11. python约瑟夫环算法和流程图_约瑟夫环问题及python与c++实现效率对比
  12. boost的编译和使用(window下)
  13. 第26条:优先考虑泛型
  14. 熊乃学 计算机,吴谋博士研究成果在权威期刊在线发表
  15. 2020年1月使用Mac线刷红米Note3全网通
  16. 常用1寸、2寸照片标准尺寸
  17. django实现websocket作为安卓开发后台(软件课设,oo聊天)
  18. 泛在操作系统实践案例
  19. 电脑某一个文件夹嵌套太多,导致无法删除的解决方法
  20. 主板装机测试软件,主板检测工具:PSPident v0.74.1版发布

热门文章

  1. python绘制糖葫芦_python简单爬虫(一)
  2. 未能连接到许可证服务器 发生错误 -96,ug启动许可证错误怎么办 UG软件启动许可证错误96解决方法...
  3. 升级 项目跑不起来_不做“睡城”!嘉定新城“十四五”加速度,产城融合跑起来,远香湖,看好你...
  4. 服务器安装三节点RabbitMQ集群(3)
  5. [UE4]导入 PSD 文件失败的解决办法:输出为 PNG,将 PNG 再保存为 PSD 导入
  6. python 遍历数组根据规律拆分,python 实现以相同规律打乱多组数据
  7. python iloc iat_Python Pandas Dataframe.iat[ ]用法及代码示例
  8. docker 使用技巧
  9. excel填充序列_what?你的excel可以按照班级填充序列?
  10. 1539. 第 k 个缺失的正整数