Given an integer, write a function to determine if it is a power of two.

2的幂的二进制表示中,必定仅仅有一个“1”,且不可能为负数。

class Solution {
public:
bool isPowerOfTwo(int n) {
if(n<0)
{//若为负数则直接返回
return false;
}
int num=0;
while(n)
{//统计1的个数
n=n&(n-1);
num++;
}
if(num==1)
return true;
return false;
}
};

leetCode(32):Power of Two的更多相关文章

  1. [LeetCode] 342. Power of Four 4的次方数

    Given an integer (signed 32 bits), write a function to check whether it is a power of 4. Example:Giv ...

  2. leetcode 326. Power of Three(不用循环或递归)

    leetcode 326. Power of Three(不用循环或递归) Given an integer, write a function to determine if it is a pow ...

  3. [LeetCode] 231. Power of Two 2的次方数

    Given an integer, write a function to determine if it is a power of two. Example 1: Input: 1 Output: ...

  4. [LeetCode] 231 Power of Two && 326 Power of Three && 342 Power of Four

    这三道题目都是一个意思,就是判断一个数是否为2/3/4的幂,这几道题里面有通用的方法,也有各自的方法,我会分别讨论讨论. 原题地址:231 Power of Two:https://leetcode. ...

  5. LeetCode 342. Power of Four (4的次方)

    Given an integer (signed 32 bits), write a function to check whether it is a power of 4. Example:Giv ...

  6. LeetCode 342. Power of Four

    Given an integer (signed 32 bits), write a function to check whether it is a power of 4. Example:Giv ...

  7. 【LeetCode】Power of Two

    问题描写叙述 Given an integer, write a function to determine if it is a power of two. 意:推断一个数是否是2的n次幂 算法思想 ...

  8. leetcode:Power of Two

    Given an integer, write a function to determine if it is a power of two. 分析:这道题让我们判断一个数是否为2的次方数(而且要求 ...

  9. Python [Leetcode 342]Power of Four

    题目描述: Given an integer (signed 32 bits), write a function to check whether it is a power of 4. Examp ...

随机推荐

  1. php执行运算符

    php执行运算符 简介 php 支持一个执行运算符:反引号(``).反引号(``)位于键盘Tab键左上方.php 将尝试将反引号中的内容作为外壳命令来执行,并将其输出信息返回(例如,可以赋给一个变量而 ...

  2. B1260 [CQOI2007]涂色paint 区间dp

    这个题和我一开始想的区别不是很大,但是要我独自做出来还是有一些难度. 每一次涂色 只有这两种可能: 1) 把一段未被 覆盖过的区间 涂成 * 色 2) 把一段被一种颜色覆盖的区间涂成 * 色 (并且 ...

  3. HttpServletRequest对象小结

    当客户端通过HTTP协议访问服务器时,请求所有信息都封装在HttpServletRequest对象中,可通过它获取到请求的所有信息,其常用方法如下: getRequestURL方法返回客户端发出请求时 ...

  4. vim下的autocmd

    AUTOCMD *autocmd.txt* For Vim version 6.2. 最后修改: 2003年3月28日 VIM 参考手册 作者:Bram Moolenaar 翻译:Zimin<c ...

  5. C - Young Physicist

    Problem description A guy named Vasya attends the final grade of a high school. One day Vasya decide ...

  6. 3.TinkPHP中的模型

    1.配置数据库的连接设置 数据库的连接配置项可以在系统的主配置文件中 2.什么是模型? 模型是MVC 三大组成部分的M,作用是负责与数据表达额交互(CRUD) 3.模型的创建 命名规范:不带前缀的标明 ...

  7. WCF与 Web Service的区别是什么?各自的优点在哪里呢?

    这是很多.NET开发人员容易搞错的问题.面试的时候也经常遇到,初学者也很难分快速弄明白 Web service: .net技术中其实就指ASP.NET Web Service,用的时间比较长,微软其实 ...

  8. SQL Server-聚焦过滤索引提高查询性能

    前言 这一节我们还是继续讲讲索引知识,前面我们讲了聚集索引.非聚集索引以及覆盖索引等,在这其中还有一个过滤索引,通过索引过滤我们也能提高查询性能,简短的内容,深入的理解,Always to revie ...

  9. es6学习 http://es6.ruanyifeng.com/

    基础学习   http://es6.ruanyifeng.com/  够了 1字符串 字符串的遍历器接口 for (let codePoint of 'foo') { console.log(code ...

  10. ES6的let命令

    1.let命令所在的代码块内有效: 2.循环的计数器,就很合适使用let命令:(for循环还有一个特别之处,就是循环语句部分是一个父作用域,而循环体内部是一个单独的子作用域.) 例如:如下会输出十次s ...