(LeetCode 231)Power of Two
来源:互联网 发布:一战往事知乎 编辑:程序博客网 时间:2024/04/28 08:14
题:
Given an integer, write a function to determine if it is a power of two.
也就是判断一个数是不是2的幂次方
solution:
这道题解法很多,最简单的就是循环。
已知2的幂次方的二进制中有且只有一位为1。
我们向右移动,直到当前位为1。再在当前状态右移一位,如果是剩余位都是零那么就是2的幂次方。
class Solution {public: bool isPowerOfTwo(int n) { int i = 0; int m = 1; while(n>>i) { if((n>>i)&1==1){ m=n>>(i+1); break; }; i++; } if(m==0) return true; return false; }};
1 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
- 福州大学第十二届程序设计竞赛-G - Escape
- 【HDU】2041-超级楼梯
- 如何阅读一本书(5)
- SPOJ 1812 LCS2 后缀自动机
- 文件上传目录(哈希目录)
- (LeetCode 231)Power of Two
- gen_server
- 再聊用strace来定位没有core文件的core dump
- 福州大学第十二届程序设计竞赛-H - 最小花费
- HashSet下的字符串排序,关键字,HashSet转TreeSet()
- flask安装mysql-python出错(环境ubuntu14.04)
- 高级工程师面试经验宝典
- XXE漏洞——XML实体定义引起的兼容性问题
- HDU 4416 Good Article Good sentence 后缀自动机