LintcodeO(1)时间检测2的幂次
来源:互联网 发布:云南山歌软件下载 编辑:程序博客网 时间:2024/06/05 13:40
O(1)时间检测2的幂次
用 O(1) 时间检测整数 n 是否是 2 的幂次。
样例
n=4
,返回 true
;
n=5
,返回 false
.
注意:判断是否为2的幂次的方法是n&(n-1),因为2的幂次的二进制表示为1后面接很多零,而减去1后为0后面接很多1,按位与后就全为0了。
public class Solution {
/*
* @param n: An integer
* @return: True or false
*/
public boolean checkPowerOf2(int n) {
boolean a=false;
// write your code here
if(n>0&&n&(n-1)==0){
a=true;
}
return a;
}
}
阅读全文
0 0
- LintcodeO(1)时间检测2的幂次
- 142-时间检测2的幂次
- LintCode 142. O(1)时间检测2的幂次
- O(1)时间检测2的幂次
- LintCode:O(1)时间检测2的幂次
- O(1)时间检测2的幂次
- LintCode :O(1)时间检测2的幂次
- Java实现-O(1)时间检测2的幂次
- O(1)时间检测2的幂次
- O(1)时间检测2的幂次-LintCode
- 2的次方(去哪儿2017校招真题) & O(1)时间检测2的幂次(LintCode)
- 用 O(1) 时间检测整数 n 是否是 2 的幂次。
- 比特位操作——O(1)时间检测2的幂次
- O(1)检测2的幂次
- O次(1)检测2的幂
- LintCode_O(1)检测2的幂次
- 142.O(1) Check Power of 2-O(1)时间检测2的幂次(容易题)
- python_lintcode_64合并排序数组 II_60搜索插入位置_142 O(1)时间检测2的幂次
- bzoj1485 [HNOI2009]有趣的数列(Catalan)
- Stacking Learning在分类问题中的使用
- Eclipse Neon 意外退出后,启动页卡在 org.eclipse.m2e.core
- 一个企业如果对未来有很强信心的话,他应该用债券不应该用股票,只有企业觉得下一步的前景暗淡的时候才会用股票
- 跳槽季:分享从事C#开发人员面试经验
- LintcodeO(1)时间检测2的幂次
- UVA11582 巨大的斐波那契数!
- Linux系统下-进程间通信(消息队列-详解)
- C语言算法6-15
- C语言基础知识学习(三)
- NOIP 2002普及组 过河卒详解
- Servlet中文乱码问题及其解决方法
- Currency Exchange(POJ1860 floyd)
- 关于golang的defer、 return、返回值三者的执行逻辑