Leetcode:231. Power of Two(JAVA)
来源:互联网 发布:梦幻西游挂机软件 编辑:程序博客网 时间:2024/05/07 19:06
【题目描述】
Given an integer, write a function to determine if it is a power of two.
Follow up:
Could you do it without using any loop / recursion?
不用循环和递归,则:
2^x=n
log(2^x) = log(n)
x log(2) = log(n)
x = log(n) / log(2)
由于JAVA double浮点型的问题,需判断Math.abs(x - Math.round(x)) < 10e-15public class Solution { public boolean isPowerOfThree(int n) { double temp = 10e-15; if(n==0) return false; double res = Math.log(n) / Math.log(2); return Math.abs(res-Math.round(res)) < temp; }}
0 0
- Leetcode:231. Power of Two(JAVA)
- Power of Two LeetCode Java
- 【leetcode】Power of Two【java】
- LeetCode Power of Two JAVA
- [leetcode] 231.Power of Two
- LeetCode ||231. Power of Two
- [LeetCode]231.Power of Two
- Leetcode 231. Power of Two
- leetcode 231. Power of Two
- 231. Power of Two LeetCode
- [LeetCode]231. Power of Two
- [LeetCode]231. Power of Two
- LeetCode 231. Power of Two
- [leetcode] 231. Power of Two
- leetcode 231. Power of Two
- LeetCode *** 231. Power of Two
- leetcode 231. Power of Two
- LeetCode 231. Power of Two
- 使用JDK自带jvisualvm监控tomcat
- VS调试去掉警告
- 答案比对
- AndroidDesignSupportLibrary 之 TabLayout
- caffe学习笔记2_用一个预训练模型提取特征
- Leetcode:231. Power of Two(JAVA)
- Ubuntu14.04编译Webrtc
- <aop:aspectj-autoproxy proxy-target-class="false"/>导致出现404状态码
- 学习笔记——shell命令(1)GREP
- Android下的仿iOS搜索 - SearchBar
- 数组循环右移
- 像人类大脑一样的欺骗检测架构设计
- 一种std::string的格式化方法
- Cocoa对象 根类