LeetCode 231 Power of Two
来源:互联网 发布:上座部佛教 知乎 编辑:程序博客网 时间:2024/06/03 15:44
Given an integer, write a function to determine if it is a power of two.
AC代码如下:
class Solution {public: bool isPowerOfTwo(int n) { if(n<=0) return false; int count=0; for(int i=0;i<sizeof(int)*8;i++){ if(n>>i&1==1) count++; if(count>=2) return false; } return true; }};看了大牛的解答,后自愧不如,代码如下:
class Solution {public: bool isPowerOfTwo(int n) { return n > 0 && !(n&(n-1)); }};
0 0
- 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
- Power of Two(leetcode 231)
- 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】231Power of Two
- 【LeetCode】231Power of Two
- LeetCode 231 Power of Two
- LeetCode 231:Power of Two
- 浅谈node.js 回调函数与事件机制
- CCFMarkDown
- 2333
- java 操作Redis Set 命令 实例(Jedis) (五)
- Linux系统图形化安装教程
- LeetCode 231 Power of Two
- SuperMap iMobile for Android 三维使用流程说明
- Android/Java的开源项目收藏(各行业)
- Ceph RGW远程同步(multisite)机制研究
- android6.0获得运行时权限方法抽取
- LeetCode K sum问题总结
- 设置并使用树莓派进行Python和C语言编程 (下)
-  ; 都表示空格,但是使用有区别
- Q&A——渲染优化