Using O(1) time to check whether an integer n is a power of 2.
Example
For n=4, return true For n=5, return false Challenge
O(1) time Tags Expand

这道题考察bit manipulation. 1的个数只能有1个才是power of 2. 主要是要注意Integer.MIN_VALUE,这个只有一个1,但是是false

 class Solution {
/*
* @param n: An integer
* @return: True or false
*/
public boolean checkPowerOf2(int n) {
// write your code here
boolean one = false;
for (int i=0; i<31; i++) {
if ((n>>>i & 1) == 0) continue;
else if (!one) one = true;
else return false;
}
if (one) return true;
else return false;
}
};

Lintcode: O(1) Check Power of 2的更多相关文章

  1. 142. O(1) Check Power of 2【easy】

    142. O(1) Check Power of 2[easy] Using O(1) time to check whether an integer n is a power of 2. Have ...

  2. O(1) Check Power of 2 - LintCode

    examination questions Using O(1) time to check whether an integer n is a power of 2. Example For n=4 ...

  3. 142. O(1) Check Power of 2【LintCode by java】

    Description Using O(1) time to check whether an integer n is a power of 2. Example For n=4, return t ...

  4. LintCode刷题笔记-- O(1) Check Power of 2

    标签: 位运算 题目: Using O(1) time to check whether an integer n is a power of 2. 解题思路: 这道题是利用位运算判断一个数是不是2 ...

  5. [LintCode]——目录

    Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...

  6. Power of Two & Power of Three & Power of Four

     Check Power of 2 Using O(1) time to check whether an integer n is a power of 2. Example For n=4, re ...

  7. lintcode:1-10题

    难度系数排序,容易题1-10题: Cosine Similarity new  Fizz Buzz  O(1)检测2的幂次  x的平方根  不同的路径  不同的路径 II  两个字符串是变位词  两个 ...

  8. LintCode刷题笔记-- Count1 binary

    标签: 位运算 描述: Count how many 1 in binary representation of a 32-bit integer. 解题思路: 统计一个int型的数的二进制表现形式中 ...

  9. DELL_LCD错误提示代码

    代码 文本 原因E1000 Failsafe voltage error. Contact support.(故障保护电压错误.请联络支持人员.) 查看系统事件记录以了解严重故障事件.E1114 Am ...

随机推荐

  1. json一些特点

    1:mina框架中客户端信息到服务端信息的输出格式为json, 原因:json通过key-value对的集合,和xml相比,json数据的体积更加小,传输效率高,容易解析 缺点:可读性不高 2:

  2. C#打开一个文本文件并读写

    OpenFileDialog OFD = new OpenFileDialog(); OFD.Title = "打开第一个文本文件"; OFD.FileName = "* ...

  3. StartSSL免费证书申请笔记

    第一步:申请startssl账号 填写相应信息后,你所填写的邮箱会收到邮件 里面有一个用来验证的验证码 输入得到的.... 注册成功后会安装数字证书(注意:注册过程中没有叫输入账号密码,这也是通过证认 ...

  4. memache session

    Memcache和PHP memcach扩展安装请见http://koda.iteye.com/blog/665761 设置session用memcache来存储 方法I: 在 php.ini 中全局 ...

  5. find grep 组合使用

    1. 查找所有".h"文件 find /PATH -name "*.h" 2. 查找所有".h"文件中的含有"helloworld ...

  6. Ubuntu 14.04 在桌面上双击运行shell 脚本文件

    http://askubuntu.com/questions/465531/how-to-make-a-shell-file-execute-by-double-click up vote7down ...

  7. qt cef嵌入web

    原文http://blog.sina.com.cn/s/blog_9e59cf590102vnfc.html 最近项目需要,研究了下libcef库. Cef(Chromium Embedded Fra ...

  8. qt如何实现一个渐隐窗口呢(开启的时候他是从上往下渐渐显示)

    qt如何实现一个渐隐窗口呢?就是比如说开启的时候他是从上往下渐渐显示的,关闭的时候从下往上渐渐小时的http://stackoverflow.com/questions/19087822/how-to ...

  9. 新的开始--Python

    第一周——python基础 1.Python简介 1.1Python简史 1.2安装Python 1.3“hello world” 在运行第一个程序“hello world”之前,先来看看运行Pyth ...

  10. Ajax如何实现跨域问题

    一个域名的组成 http:// www . abc.com : 8080 /scripts/jquery.js 协议 子域名 主域名 端口号 请求资源地址 当协议.子域名.主域名.端口号中任意一个不同 ...