题意:判断1个数n是否刚好是2的幂,幂大于0。

思路:注意会给负数,奇数。对于每个数判断31次即可。

 class Solution {
public:
bool isPowerOfTwo(int n) {
if(n<||(n&)==&&n>) return false;
unsigned int t=;
while(t<n) //注意爆int
t<<=;
if(t==n) return true;
return false;
}
};

AC代码

一行代码,更巧的写法:

  如果一个数n是2的某次幂,那么他应该是100000这样的,那么n-1会是0111111这样的,两者相与n&(n-1)必定为0。

 class Solution {
public:
bool isPowerOfTwo(int n) {
return n> && !(n&(n-)) ;//100和011的&应该是0才对
}
};

AC代码

LeetCode Power of Two (2的幂)的更多相关文章

  1. C#LeetCode刷题之#342-4的幂(Power of Four)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4058 访问. 给定一个整数 (32 位有符号整数),请编写一个函 ...

  2. C#LeetCode刷题之#326-3的幂(Power of Three)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3867 访问. 给定一个整数,写一个函数来判断它是否是 3 的幂次 ...

  3. C#LeetCode刷题之#231-2的幂(Power of Two)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3858 访问. 给定一个整数,编写一个函数来判断它是否是 2 的幂 ...

  4. [LeetCode] Power of Four 判断4的次方数

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

  5. [LeetCode] Power of Three 判断3的次方数

    Given an integer, write a function to determine if it is a power of three. Follow up:Could you do it ...

  6. [LeetCode] Power of Two 判断2的次方数

    Given an integer, write a function to determine if it is a power of two. Hint: Could you solve it in ...

  7. Leetcode Power of Two

    Given an integer, write a function to determine if it is a power of two. 题目意思: 给定一个整数,判断是否是2的幂 解题思路: ...

  8. LeetCode Power of Four

    原题链接在这里:https://leetcode.com/problems/power-of-four/ 题目: Given an integer (signed 32 bits), write a ...

  9. LeetCode Power of Three

    原题链接在这里:https://leetcode.com/problems/power-of-three/ 与Power of Two类似.检查能否被3整除,然后整除,再重复检查结果. Time Co ...

随机推荐

  1. sql新感悟(where 1 = 1)

    花了好久把YII框架看完发现一本很不错的书:SQL案例解析(清华大学出版社),看到一些比较有用的东西,感觉应该把他记录下来,看了好多页发现书中一直有 where 1=1,这样的语句,查过发现“wher ...

  2. Python数据结构——二叉树的实现

    1. 二叉树 二叉树(binary tree)中的每个节点都不能有多于两个的儿子. 1.1 二叉树列表实现 如上图的二叉树可用列表表示: tree=['A', #root ['B', #左子树 ['D ...

  3. 第1章 Git的版本控制之道

    版本控制系统(Version Control System,VCS)可以帮助我们记录和跟踪项目中各文件内容的修改变化. 1.1 版本库 版本库(Repository)是版本控制系统用来存储所有历史数据 ...

  4. ios 基于CAEmitterLayer的雪花,烟花,火焰,爱心等效果demo(转)

    转载自:http://blog.csdn.net/mad2man/article/details/16898369 分类: cocoa SDK2013-11-23 11:52 388人阅读 评论(0) ...

  5. 微软职位内部推荐-Principal DEV Manager for Bing Client

    微软近期Open的职位: Title: Principal DEV Manager for Bing ClientGroup: Search Technology Center Asia, BingW ...

  6. WinForm 控件库

    1:Telerik 介绍: Telerik 是保加利亚的一个软件公司,专注于微软.Net平台的表示层与内容管理控件.Telerik 提供高度稳定性和丰富性能的组件产品,并可应用在非常严格的环境中. 现 ...

  7. Delphi XE5 如何设计并使用FireMonkeyStyle(转)

    如何设计并使用FireMonkeyStyle FireMonkey使用Style来控制控件的显示方式. 每个控件都有一个StyleLookup属性,FireMonkey就是通过控件的这个属性来在当前窗 ...

  8. Netty4.x中文教程系列(一) Hello World !

    1.下载并为项目添加Netty框架 1. Netty的包大家可以从Netty官网:http://netty.io/downloads.html 下载 如图所示: Netty提供了四个个主要版本的框架包 ...

  9. Oracle创建触发器实现主键自增

    CREATE OR REPLACE TRIGGER "trigger_empl" before insert on extjsTest1.t_empl for each row b ...

  10. spoj 416

    又臭又长的烂代码 ...... #include <iostream> #include <cstdio> #include <cstring> #include ...