Leetcode 231. Power of Two
来源:互联网 发布:stm32单片机项目大全 编辑:程序博客网 时间:2024/06/01 03:58
Given an integer, write a function to determine if it is a power of two.
方法一:
public class Solution {
public boolean isPowerOfTwo(int n) {
if(n<1)return false;
if(Integer.bitCount(n) == 1)return true;
return false;
}
}
方法二:
public class Solution {
public boolean isPowerOfTwo(int n) {
if(n<1)return false;
//return (int)Math.pow(2,Math.round(Math.log(n)/Math.log(2)))==n;
return (Math.pow(2,(int)(Math.log(Integer.MAX_VALUE)/Math.log(2)))%n == 0);
//此处强制转换为int型应该放在pow的第二个参数处,这样 才符合2的整数次幂,如果放在pow函数外面,则可能是将一个浮点型的数字转换为了int型
}
}
方法三:
public class Solution {
public boolean isPowerOfTwo(int n) {
if(n<1)return false;
return Math.pow(2,((int)Math.round(Math.log(n)/Math.log(2))))==n;
}
}//此处int强制转换理由同上
- [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
- Leetcode-231. Power of Two
- #leetcode#231. Power of Two
- 231.[Leetcode]Power of Two
- Leetcode 231. Power of Two
- hibernate防止sql语句注入
- java泛型实现链式栈
- 做技术需要匠心(转)
- LeetCode-162.Find Peak Element
- 识别有效的IP地址和掩码并进行分类统计
- Leetcode 231. Power of Two
- tcp协议详解
- ACM-–圆–-HDOJ 1065 --I Think I Need a Houseboat--水
- leetcode_c++:Combination Sum(039)
- 橫跨Android和iOS平台的多功能表單空間BHFormView
- hadoop的伪分布安装(低版本)
- WebLogic12安装过程详解
- 使用Eclipse中出现的一些问题的解决方法
- linux下各类服务器构建与使用指南