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

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

  1. class Solution {
  2. public:
  3. bool isPowerOfTwo(int n) {
  4. if(n<||(n&)==&&n>) return false;
  5. unsigned int t=;
  6. while(t<n) //注意爆int
  7. t<<=;
  8. if(t==n) return true;
  9. return false;
  10. }
  11. };

AC代码

一行代码,更巧的写法:

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

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

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. 1043. Is It a Binary Search Tree

    http://www.patest.cn/contests/pat-a-practise/1043 #include <stdio.h> #include <vector> u ...

  2. [摘] SQLPLUS Syntax

    You use the SQLPLUS command at the operating system prompt to start command-line SQL*Plus: SQLPLUS [ ...

  3. 关于EndNote X6工具文献管理以及参考文献生成的使用

    1 利用endnote下载参考文献 1.1在CNKI中查找文献,在前面打钩,这里显示已经选中两篇文献了。 然后选择导出文献——选择Endnote——导出并保存text文件 打开Endnote——imp ...

  4. oracle闪回表详解

    --- 说明闪回数据库 --- 使用闪回表将表内容还原到过去的特定时间点 --- 从删除表中进行恢复 --- 使用闪回查询查看截止到任一时间点的数据库内容 --- 使用闪回版本查询查看某一行在一段时间 ...

  5. RAC环境下SCAN IP可以PING通,1521端口也可以TELNET,但是无法建立数据库连接

    昨天用户请求帮助处理一个问题:有个厂家需要连某个业务系统的数据库,网络上已经开通了权限,SCAN IP可以PING通,测试TELNET 1521端口也是正常.但是想通过SQLPLUS连接,总是会提示连 ...

  6. 【git】学习路径失败了

    期初规划:搭建git远程服务器  使用gitlab作为管理工具 过程遇到的问题 1.gitlab不能安装到win ,且对centos要求6以上,我只有一台centos5  让运维帮升级 ...等待.. ...

  7. 彻底卸载网易UU网游加速器的方法

    昨天跟朋友一起玩游戏,网速感觉不怎么好就下了一个免费的网易UU加速器来给对战平台加速,结果加速了以后网速更差,我晕,于是想卸载,可这个加速器口只有一个exe文件,不用安装,但在第一次加速时记得安装了一 ...

  8. Linux与Windows中动态链接库的分析与对比

    摘要:动态链接库技术实现和设计程序常用的技术,在Windows和Linux系统中都有动态库的概念,采用动态库可以有效的减少程序大小,节省空间,提高效率,增加程序的可扩展性,便于模块化管理.但不同操作系 ...

  9. submit和button提交表单的区别

    <html> <meta http-equiv="Content-Type" content="text/html; charset=utf-8&quo ...

  10. "Principles of Reactive Programming" 之<Actors are Distributed> (1)

    week7中的前两节课的标题是”Actors are Distributed",讲了很多Akka Cluster的内容,同时也很难理解. Roland Kuhn并没有讲太多Akka Clus ...