【LeetCode】231. Power of Two
2015-07-08 09:49
351 查看
Power of Two
Given an integer, write a function to determine if it is a power of two.
Credits:
Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.
2的次幂的特征就是,二进制表示中只有一位是1.
乘以2相当于左移一位,因此2^k的二进制相当于把1(2^0)向左移动k次
Given an integer, write a function to determine if it is a power of two.
Credits:
Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.
2的次幂的特征就是,二进制表示中只有一位是1.
乘以2相当于左移一位,因此2^k的二进制相当于把1(2^0)向左移动k次
class Solution { public: bool isPowerOfTwo(int n) { if(n <= 0) return false; return (n & (n-1)) == 0; } };
相关文章推荐
- 关于NSTimer的几点说明
- HTTP深入浅出 http请求
- sql中内连接与外连接的区别
- KVM虚拟化存储池管理
- Algorithms—41.First Missing Positive
- 观念的水位
- 被AppStore拒绝理由(一)
- 软件需求书籍推荐
- 数组变成集合形式
- (转)在Windows平台上安装Node.js及NPM模块管理
- Apache的status相关问题解决办法
- android4.4.+状态栏改变颜色
- JavaScript forEach()遍历函数使用及介绍
- Got fatal error 1236 from master when reading data from binary log
- 黑马程序员————第十八天
- 访问FTP站点下载文件,提示“当前的安全设置不允许从该位置下载文件”
- qt 正则表达式
- alter table 常见语法
- BaseExpandableListAdapter的用法
- 排序算法!~