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. EOJ 2844 排序去重

    有 n 个 1 到 1000 之间的整数 (1≤n≤100),对于其中重复的数字,只保留一个,把其余相同的数去掉.然后再按照指定的排序方式把这些数排序. Input 第 1 行为字母 A 或 D,A ...

  2. 27. Remove Element[E]移除元素

    题目 Given an array nums and a value val, remove all instances of that value in-place and return the n ...

  3. 使用autofac在mvc5下依赖注入

    把遇到的问题汇总一下: 一.安装mvc5版本 命令:pm> Install-Package Autofac 结果安装的Autofac.Integration.Mvc(版本为4.0),所引用的依赖 ...

  4. Scrapy中的UA池,代理池,以及selenium的应用

    UA池 代理池 selenium在Scrapy中的应用 UA池 - 下载中间件: - 下载中间件(Downloader Middlewares) 位于scrapy引擎和下载器之间的一层组件. - 作用 ...

  5. 异步lambda表达式

  6. 6.Renderer Window

    渲染是实时的,所见即所得.同时还可以输出一些统计信息. Pixel Snoop:获取颜色值,同时把该值复制到剪贴板,主要用户是获取颜色值 Wireframe:开启后可以查看3D节点图形骨架 Stati ...

  7. 实现model中的文件上传FTP(二)

    上一篇博客记录了如何将model中的图片存入FTP,通过一个第三方的storages简单的实现了,但是后续我发现如果想在浏览器通过url直接获取图片,就不太容易了(大神轻喷,小弟自学django和py ...

  8. SpringMVC(一) HelloWorld

    学习新东西的的第一个程序--HelloWorld,以下是SpringMVC的HelloWorld 第一步: 用MAVEN 创建webapp,并添加依赖.(强烈建议使用MAVEN,MAVEN学习书籍和视 ...

  9. 创建一个dynamics CRM workflow (四) - Development of Custom Workflows

    首先我们需要确定windows workflow foundation 已经安装. 创建之后先移除MyCustomWorkflows 里面的 Activity.xaml 从packages\Micro ...

  10. form表单提交三种方式,demo实例详解

    第一种:使用type=submit  可以直接提交 <html> <head> <title>submit直接提交</title> </head& ...