231. Power of Two
来源:互联网 发布:詹姆斯哈登数据 编辑:程序博客网 时间:2024/05/21 09:12
Given an integer, write a function to determine if it is a power of two.
class Solution {public: bool isPowerOfTwo(int n) { return (n>0)&&!(n&(n-1)); }};
首先需要n大于0,然后n和n-1进行与处理,如果n是2的指数,那么n&(n-1)返回的是0,也就是false,这时需要在前面加个非。
0 0
- 231. Power of Two
- 231. Power of Two
- 231. Power of Two
- 231. Power of Two
- 231. Power of Two
- 231. Power of Two
- 231. Power of Two
- 231. Power of Two
- 231. Power of Two
- 231. Power of Two
- 231. Power of Two
- 231. Power of Two
- 231. Power of Two
- 231. Power of Two
- 231. Power of Two
- 231. Power of Two
- 231. Power of Two
- 231. Power of Two
- 1020. 月饼
- 巧用Snackbar代替Toast
- pywin32
- 文章标题
- jdbc链接mysql数据库
- 231. Power of Two
- 124. Binary Tree Maximum Path Sum
- C++浅拷贝的另一种实现方式
- left join 中on和where的讲解
- 学习内容
- [RegEx]识别路径中是否有本地盘符
- 3398 数据结构实验之排序一:一趟快排
- Graphics中的方法
- replace和replaceAll的区别: