[LeetCode]231. Power of Two
来源:互联网 发布:ubuntu如何识别u盘 编辑:程序博客网 时间:2024/05/19 20:48
原题目
题目
Given an integer, write a function to determine if it is a power of two.
检测一个数是否是2的次方
public class Solution { public boolean isPowerOfTwo(int n) { int cnt = 0; while(n > 0) { cnt += n & 1; n >>= 1; } return cnt == 1; }}
优先考虑用位运算
2的次方的数规律:在2进制下只有1个1,其余位都是0
每次将二进制右移一位,统计出现多少个1
&运算符 :只要两个表达式在某一位上都是 1,则结果的该位也是 1。否则,结果的该位是 0。
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
- 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
- Qt获取控件位置,坐标总结
- Dash 安装 IOS文档 Apple 开发者离线文档下载
- Pycharm创建文件模板
- Qt查找子窗口
- 最全的iOS面试题及答案
- [LeetCode]231. Power of Two
- Qt tip——初始化窗体Geometry()
- MyEclipse使用Maven创建web项目+搭建SSM框架教
- 使用canvas.clipPath来裁剪时部分机型出现问题
- vim配置及插件安装管理
- leetcode-139-Word Break
- NIST: Report on Post-Quantum Cryptography
- Mybatis的Spring配置
- UVA-11383 (KM算法)