Lintcode: O(1) Check Power of 2
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的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- LintCode刷题笔记-- O(1) Check Power of 2
标签: 位运算 题目: Using O(1) time to check whether an integer n is a power of 2. 解题思路: 这道题是利用位运算判断一个数是不是2 ...
- [LintCode]——目录
Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...
- 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 ...
- lintcode:1-10题
难度系数排序,容易题1-10题: Cosine Similarity new Fizz Buzz O(1)检测2的幂次 x的平方根 不同的路径 不同的路径 II 两个字符串是变位词 两个 ...
- LintCode刷题笔记-- Count1 binary
标签: 位运算 描述: Count how many 1 in binary representation of a 32-bit integer. 解题思路: 统计一个int型的数的二进制表现形式中 ...
- DELL_LCD错误提示代码
代码 文本 原因E1000 Failsafe voltage error. Contact support.(故障保护电压错误.请联络支持人员.) 查看系统事件记录以了解严重故障事件.E1114 Am ...
随机推荐
- Linguistic corpora 种子语料库-待分析对象-分析与更新语料库
Computational Linguistics http://matplotlib.org/ https://github.com/matplotlib/matplotlib/blob/maste ...
- Types of compression algorithms
http://www.html5rocks.com/en/tutorials/speed/img-compression/ Types of compression algorithms There ...
- xinwajueji
#include<stdio.h> int map[10][10]={0}; int step[30]={0}; int max=99999; int ans[99]={0}; int ...
- [转]SQLBulkCopy使用
SQLBulkCopy,用于数据库之间大批量的数据传递.通常用于新,旧数据库之间数据的更新.即使表结构完全不同,也可以通过字段间的对应关系,顺利的将数据导过来. 首先,SQLBulkCopy需要2个连 ...
- SQL查询中关于索引使用的笔记
建表KeyLevelStat (无主键),2个索引: CREATE TABLE KeyLevelStat( [Date] [int] NOT NULL, [Num] [varchar](8), [R0 ...
- JAVA缓存技术之EhCache
最近再ITEYE上看到关于讨论JAVA缓存技术的帖子比较多,自己不懂,所以上网大概搜了下,找到一篇,暂作保存,后面如果有用到可以参考.此为转贴,帖子来处:http://cogipard.info/ar ...
- java多线程编程(二创建线程)
1.概念 因为java是完全面向对象的,所以在java中,我们说的线程,就是Thread类的一个实例对象.所以,一个线程就是一个对象,它有自己字段和方法. 2.创建线程 创建线程有 ...
- Haskell解决逆波兰式
摘自<Haskell趣学指南- Learn You a Haskell for Great Good> {- 逆波兰式(revese polish notation, RPN): 操作符出 ...
- 动态创建地图文档MXD并发布地图服务
原文:动态创建地图文档MXD并发布地图服务 1.动态创建MXD private bool CreateMxd(string MxdPath, string MxdName) { IMapDocumen ...
- Wall---hdu1348(求凸包周长 模板)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1348 求凸包周长+2*PI*L: #include <stdio.h> #include ...