Power of Three

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?

https://leetcode.com/problems/power-of-three/


要求判断一个数是不是3的幂次方。

最直观的做法是不停除以3,看余数是否为零。

题目要求不能用递归和循环,那就是数学题了。

把给定的数N用求以3为底的对数(log3N),如果结果是整数说明N是3的幂次方。

1 /**
2  * @param {number} n
3  * @return {boolean}
4  */
5 var isPowerOfThree = function(n) {
6     n = Math.log(n) / Math.log(3);
7     return Math.abs(n - Math.round(n)) < 1e-10 ? true : false;
8 };

转载于:https://www.cnblogs.com/Liok3187/p/5181164.html

[LeetCode][JavaScript]Power of Three相关推荐

  1. [leetcode] 342. Power of Four

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

  2. LeetCode:326. Power of Three

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

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

  4. leetcode 231. Power of Two

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

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

  6. LeetCode之Power of Two

    1.题目 Given an integer, write a function to determine if it is a power of two. Credits: Special thank ...

  7. [LeetCode][JavaScript]Roman to Integer

    Roman to Integer Given a roman numeral, convert it to an integer. Input is guaranteed to be within t ...

  8. LeetCode 326. Power of Three

    题目: 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(c语言)

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

最新文章

  1. 协作工具 discord 和 slack
  2. ASP.Net MVC如何访问的静态页面
  3. JAVA代码如何实现删除文件夹6_java文件创建、删除、读取、写入操作大全
  4. React Native
  5. 采购申请 RFC BAPI
  6. [Spring]-各种标注-零配置
  7. P5221 Product(反演)
  8. cd返回上一 git_使用Git实现自动化部署项目
  9. verilog实现多周期处理器之——(六)简单算数操作指令的实现
  10. intellij 出现“Usage of API documented as @since 1.8+”的解决办法
  11. C语言程序设计 学习笔记
  12. 吴恩达深度学习课后作业-目标检测的环境配置
  13. Camera IIC总线
  14. 《CLR via C#》读书笔记-.NET多线程(一)
  15. selenium driver关闭
  16. vscode 推荐premiter_精选!15 个必备的 VSCode 插件
  17. 苹果cms怎么采集别人网站的视频
  18. 华为与这三所一流大学又有新互动!
  19. 短序列比对利器-bwa
  20. 实现opencv在图片上画框准确定位某个主体

热门文章

  1. centos php 开启libgdgd_CentOS6.5安装Nginx1+MySQL5+PHP5
  2. 不同工作组能访问吗_「长笛众测」你能听出3K-9W不同价位长笛的区别吗?
  3. ffmpeg 2.6.3在Linux下的编译
  4. GBK转unicode码查询表
  5. 【Java】图解 Java 中的数据结构及原理
  6. SparkSQL下UDF的使用
  7. 【Maven】Maven classifier的作用
  8. Spring : 缓存相关注解@EnableCaching、@CacheConfig、@Cacheable、@Caching
  9. hadoop基本思想与概念
  10. map、forEach与filter实例详解