题意:判断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. db2使用Java存储过程实现MD5函数

    1.数据库版本 2.Java脚本 import java.security.MessageDigest; import COM.ibm.db2.app.UDF; public class MD5UDF ...

  2. 【Spring-boot多数据库】Spring-boot JDBC with multiple DataSources sample

    application.properties spring.ds_items.driverClassName=org.postgresql.Driver spring.ds_items.url=jdb ...

  3. 【BZOJ 1087】[SCOI2005]互不侵犯King

    Description 在N×N的棋盘里面放K个国王,使他们互不攻击,共有多少种摆放方案.国王能攻击到它上下左右,以及左上左下右上右下八个方向上附近的各一个格子,共8个格子. Input 只有一行,包 ...

  4. python学习笔记15(面向对象编程)

    虽然Python是解释性语言,但是它是面向对象的,能够进行对象编程. 一.如何定义一个类 在进行python面向对象编程之前,先来了解几个术语:类,类对象,实例对象,属性,函数和方法. 类是对现实世界 ...

  5. kindeditor-4.1.10 结合 Asp.Net MVC 添加图片功能

    KindEditor是一套开源的HTML可视化编辑器,现在我要结合Asp.Net MVC4 上传图片功能,做相应的配置和修改, 其实网上也有人写过类似的文章了,我写出来是以防以后使用的时候出现这样的问 ...

  6. csu 1305 Substring (后缀数组)

    http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1305 1305: Substring Time Limit: 2 Sec  Memory Limi ...

  7. 1199: [HNOI2005]汤姆的游戏 - BZOJ

    Description 汤姆是个好动的孩子,今天他突然对圆规和直尺来了兴趣.于是他开始在一张很大很大的白纸上画很多很多的矩形和圆.画着画着,一不小心将他的爆米花弄撒了,于是白纸上就多了好多好多的爆米花 ...

  8. maven 编译部署src/main/java下的资源文件

    maven 编译部署src/main/java下的资源文件 maven默认会把src/main/resources下的所有配置文件以及src/main/java下的所有java文件打包或发布到targ ...

  9. spring的配置模式与注解模式基础

    “依赖注入”是spring的核心特征,在Web服务器(如Tomcat)加载时,它会根据Spring的配置文件中配置的bean或者是通过注解模式而扫描并装载的bean实例自动注入到Application ...

  10. PAT-乙级-1036. 跟奥巴马一起编程(15)

    1036. 跟奥巴马一起编程(15) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 美国总统奥巴马不仅呼吁所有人 ...