Leetcode231 Power of Two
2015-09-01 23:08
375 查看
Given an integer, write a function to determine if it is a power of two.
public class Solution231 { public boolean isPowerOfTwo(int n) { if(n==1) return true; if(n%2 == 1) return false; int cnt = 2; while (cnt <= Integer.MAX_VALUE) { if((cnt ^ n) == 0) { return true; } if(cnt > n || cnt <= 0) return false; cnt *= 2; } return false; } public static void main(String[] args) { Solution231 ans = new Solution231(); System.out.println(ans.isPowerOfTwo(6)); } }
相关文章推荐
- 求出1-100之间所有的素数(只能被1和它本身整除),要求每5个换行输出。
- h5地理位置对象navigator.geolocation
- 读jQuery之六(缓存数据)
- SPOJ 375 树链剖分学习
- android中所有颜色代码
- 不可变字典NSDictionary 和可可变字典NSMutableDictionary
- OneAlert:国内首家 SaaS 模式的云告警平台
- Android中关于空指针异常的解决办法
- Highcharts双饼图使用实例
- RaidoButton产生的bug
- uva1585 - Score
- linux安装配置SVN并设置钩子
- OneAlert:国内首家 SaaS 模式的云告警平台
- java IO 刘 File 基础
- 卡特兰数应用
- java 的接口使用
- java9-1
- 查询比a大的数中的第k大的数 树状数组 hdu 2852 KiKi's K-Number
- 《机器学习实战》学习笔记:k-近邻算法的两个应用场景
- VS工具:实时可视化树