Power of Two
来源:互联网 发布:王者荣耀 芈月 知乎 编辑:程序博客网 时间:2024/06/05 00:56
参考:点击打开链接
那么我们来观察下2的次方数的二进制写法的特点:
1 2 4 8 16 ....
1 10 100 1000 10000 ....
那么我们很容易看出来2的次方数都只有一个1,剩下的都是0,所以我们的解题思路就有了,我们只要每次判断最低位是否为1,然后向右移位,最后统计1的个数即可判断是否是2的次方数,代码如下:
public class Solution { public boolean isPowerOfTwo(int n) { int count = 0; while (n > 0) { count += n & 1; n >>= 1; } return count == 1; }}
0 0
- Power of Two
- [LeetCode] Power of Two
- 【LeetCode】Power of Two
- Power of Two
- power of two
- [Leetcode]Power of Two
- Power of Two
- [leetCode] Power of Two
- Power of Two
- 231Power of Two
- leetcode --Power of Two
- Power of Two
- leetcode: Power of Two
- Power of Two
- Power of Two
- Power of Two
- Leetcode46: Power of Two
- LeetCode Power of Two
- python-pandas绘图
- uva563
- codeforces 145E Lucky Queries
- POJ2533:Longest Ordered Subsequence(LIS)
- SQL注入
- Power of Two
- 使用Mybatis提供的mybatis generator来自动生成model/mapper.java/mapper.xml
- Cannot change version of project facet Dynamic web module to 2.5
- Android 性能优化之内存泄漏的检测与修复
- HDU1258 Sum It Up(DFS)
- (模板题)sdut 3374 数据结构实验之查找二:平衡二叉树(平衡二叉树的建立)
- Android内存泄漏分析及调试
- Java编程思想第4版-第二章
- 实例详解SpringMVC配置与使用