LeetCode Power of Two
来源:互联网 发布:windows ntp服务器配置 编辑:程序博客网 时间:2024/06/05 08:31
Given an integer, write a function to determine if it is a power of two.
若一个数字是2的幂,那么它的二进制表示形式里面只有一个1,那么n-1刚好是除了为1的那位之外,其余所有位都为1的数字,举个例子,4的二进制为100,而3 的二进制为011,综上,如果一个数字n&(n-1)==0那么这个数字就是2的幂。
class Solution {public: bool isPowerOfTwo(int n) { if(n<=0) return false; return (!(n&n-1)); }};
0 0
- [LeetCode] Power of Two
- 【LeetCode】Power of Two
- [Leetcode]Power of Two
- [leetCode] Power of Two
- leetcode --Power of Two
- leetcode: Power of Two
- LeetCode Power of Two
- [LeetCode] Power of Two
- LeetCode Power of Two
- [leetcode] Power of Two
- [Leetcode]Power of Two
- Leetcode: Power of Two
- [LeetCode]Power of Two
- leetcode | Power of Two
- 【leetcode】Power of Two
- [LeetCode]Power of Two
- [leetcode] Power of two
- LeetCode Power of Two
- linux面试之你认识的fork、vfork、clone
- 管道createnamepipe
- PCB布线及相关规则设置
- 认识Unix时间戳
- usaco 1-1-3 Friday the Thirteenth
- LeetCode Power of Two
- implements 和extend
- 启动Activity的两种方式startActivity和startActivityForResult(二)
- swift资料收集
- 给磁盘文件排序
- 好客户端是怎样炼成的
- easyui使用Ajax提交表单,返回Json数据
- html5 学习笔记--jikexueyuan的视频看完后的碎碎念(与HTML4相比的改变)
- 初识jquery