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. ASP.NET Razor - C# Variables

    http://www.w3schools.com/aspnet/razor_cs_variables.asp Variables are named entities used to store da ...

  2. 杂项:DS(目录服务)

    ylbtech-杂项:DS(目录服务) 1.返回顶部 1. DS(目录服务). 目录服务管理概述: 目录服务是扩展计算机系统中最重要的组件之一.虽然用户和管理通常不知道他们感兴趣对象的确切名称,但他们 ...

  3. 09.ws复杂数据类型数据传输

    和ajax的共同点是都是自己组装消息自己解析消息.这种方式的好处是一点都不用生成客户端代码.这两种方式(ajax和HttpUrlConnection)的好处是一点都不用生成客户端代码. WSDL这个文 ...

  4. [JavaEE] Apache Maven 入门篇(下)

    http://www.oracle.com/technetwork/cn/community/java/apache-maven-getting-started-2-405568-zhs.html 作 ...

  5. JDK8新特性:函数式接口

    一,定义 函数式接口,英文为Functional Interface.首先它是一个接口,那么它与其它接口有什么不同呢?不同点就是在这个接口中只允许有一个抽象方法. 这里的只允许一个抽象方法不包括以下几 ...

  6. linux 添加 msyql 开机自启动

    1.将服务文件拷贝到init.d下,并重命名为mysql cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld 2.赋予可 ...

  7. java的重载总结

    1.不能以返回值的不同来重载方法,编译都不通过(只有参数类型或者参数个数不同才可以重载方法) 在Java语言中,要重载一个方法,除了要与原方法具有相同的简单名称外,还要求必须拥有一个与原方法不同的(不 ...

  8. 如何运用docker配合python开发

    在网络层,互联网提供所有应用程序都要使用的两种类型的服务,尽管目前理解这些服务的细节并不重要,但在所有TCP/IP概述中,都不能忽略他们: 无连接分组交付服务(Connectionless Packe ...

  9. Tomcat web deploy

    环境: apache-tomcat-7.0.73 java version "1.8.0_112" 创建普通用户,使用 sudu进行操作 JDK 配置 下载地址:http://ww ...

  10. Self-hosting Sentry With Docker and Docker-compose

    If a user encounters an error but you don't know about, did it happen at all? Sentry is one of the s ...