[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:
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相关推荐
- [leetcode] 342. Power of Four
题目 Given an integer (signed 32 bits), write a function to check whether it is a power of 4.Example: ...
- LeetCode:326. Power of Three
2019独角兽企业重金招聘Python工程师标准>>> Given an integer, write a function to determine if it is a powe ...
- 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 ...
- leetcode 231. Power of Two
Given an integer, write a function to determine if it is a power of two. class Solution(object):def ...
- 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 ...
- LeetCode之Power of Two
1.题目 Given an integer, write a function to determine if it is a power of two. Credits: Special thank ...
- [LeetCode][JavaScript]Roman to Integer
Roman to Integer Given a roman numeral, convert it to an integer. Input is guaranteed to be within t ...
- LeetCode 326. Power of Three
题目: Given an integer, write a function to determine if it is a power of three. Follow up: Could you ...
- [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 ...
最新文章
- 协作工具 discord 和 slack
- ASP.Net MVC如何访问的静态页面
- JAVA代码如何实现删除文件夹6_java文件创建、删除、读取、写入操作大全
- React Native
- 采购申请 RFC BAPI
- [Spring]-各种标注-零配置
- P5221 Product(反演)
- cd返回上一 git_使用Git实现自动化部署项目
- verilog实现多周期处理器之——(六)简单算数操作指令的实现
- intellij 出现“Usage of API documented as @since 1.8+”的解决办法
- C语言程序设计 学习笔记
- 吴恩达深度学习课后作业-目标检测的环境配置
- Camera IIC总线
- 《CLR via C#》读书笔记-.NET多线程(一)
- selenium driver关闭
- vscode 推荐premiter_精选!15 个必备的 VSCode 插件
- 苹果cms怎么采集别人网站的视频
- 华为与这三所一流大学又有新互动!
- 短序列比对利器-bwa
- 实现opencv在图片上画框准确定位某个主体
热门文章
- centos php 开启libgdgd_CentOS6.5安装Nginx1+MySQL5+PHP5
- 不同工作组能访问吗_「长笛众测」你能听出3K-9W不同价位长笛的区别吗?
- ffmpeg 2.6.3在Linux下的编译
- GBK转unicode码查询表
- 【Java】图解 Java 中的数据结构及原理
- SparkSQL下UDF的使用
- 【Maven】Maven classifier的作用
- Spring : 缓存相关注解@EnableCaching、@CacheConfig、@Cacheable、@Caching
- hadoop基本思想与概念
- map、forEach与filter实例详解