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. Java单链表的实现

    将结点Node进行封装,假设Node的操作有增加,删除,查找,打印几个操作.将Node实现为链表Link的内部类,简化代码. package Chapter5; import java.securit ...

  2. Oracle存储过程基本语法 存储过程

    Oracle存储过程基本语法 存储过程 1 CREATE OR REPLACE PROCEDURE 存储过程名 2 IS 3 BEGIN 4 NULL; 5 END; 行1: CREATE OR RE ...

  3. html轮播效果的实现

    要实现如下图的效果 点击可以选择图片:不点击的时候自动轮播:并且点击完后再次自动轮播. 思路:如同在房子里透过窗子看路过的火车一样,窗子是不动的,但火车是陆续经过窗子的,所以透过窗子可以看到依次看完所 ...

  4. jboss4.2.3 SSL配置 + 生成数字签名

    一.生成数字签名 1. 生成JKS文件 keytool -genkey -keyalg RSA -alias jbosskey -keystore jbosskey.jks 在win7系统中,该文件的 ...

  5. 1014 C语言文法

    <程序> -> <外部声明> | <程序> <外部声明> <外部声明> -> <函数定义> | <声明> ...

  6. Windows下Memcache的安装与在php中使用

    memcache dll插件和测试例子下载地址: http://pecl.php.net/package/memcache Windows下Memcache的安装方法 Memcached官方:http ...

  7. Jquery调用webService的四种方法

    1.编写4种WebService方法 [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(Conf ...

  8. 通过magento后台的magento connect安装magento extension

    http://magentoinfo.blog.163.com/blog/static/215636160201302272653538/ magento的extension库基本上可以说要什么有什么 ...

  9. iOS 增加UIButton按钮的可点击区域

    在很多时候,按钮可能看起来那么大,但是在它周围进行点击时,都能够触发事件,是因为它的可点击区域比我们看到的button要大. 在使用AutoLayout的时候,我们处理的是按钮的image属性,所以这 ...

  10. JMeter学习-012-JMeter 配置元件之-HTTP Cookie管理器-实现 Cookie 登录

    前文我们讲过了若何获取登录后的 Cookie 信息,不知如何获取登录 Cookie 的朋友,敬请参阅我之前写的博文:Fiddler-005-获取 Cookie 信息.参阅上篇文章,获取到 Cookie ...