*[Lintcode]O(1) Check Power of 2
来源:互联网 发布:暴雪铁人五项 知乎 编辑:程序博客网 时间:2024/05/29 03:30
Using O(1) time to check whether an integer n is a power of2
.
Example
For n=4
, return true
;
For n=5
, return false
;
Challenge
O(1) time
若为2的幂次方,则只存在一位为1. 可以1)统计1的个数 2)与n-1做与运算,无相同位即为true
class Solution { /* * @param n: An integer * @return: True or false */ public boolean checkPowerOf2(int n) { if (n<=0) return false; boolean res = ((n & (n-1)) == 0) ? true : false; return res; }};
0 0
- lintcode: O(1) Check Power of 2
- *[Lintcode]O(1) Check Power of 2
- LintCode O(1) Check Power of 2 O(1)检测2的幂次
- [刷题]O(1) Check Power of 2
- O(1) Check Power of 2
- #142 O(1) Check Power of 2
- [Microsoft] O(1) Check Power of 2
- Lintcode142 O(1) Check Power Of 2 solution 题解
- 142.O(1) Check Power of 2-O(1)时间检测2的幂次(容易题)
- check a int number is power of 2?
- [LintCode 697] Check Sum of Square Numbers(Python)
- Next Power of 2
- 1 Power of Cryptography
- lintcode-O(1)检测2的幂次-142
- LintCode-O(1)检测2的幂次
- LintCode O(1)检测2的幂次
- LintCode 142. O(1)时间检测2的幂次
- LintCode:O(1)时间检测2的幂次
- 51Nod 1163 最高的奖励(贪心+优先队列/并查集合)
- 黑客教程
- [Lintcode]Number of Islands
- 悲观的人更容易获得好的感觉
- 人工智能技术与现代应用
- *[Lintcode]O(1) Check Power of 2
- EXCEL所有单元格加减乘除一个数 -- 选择性粘贴的用法
- 细说产品经理
- 产品经理和程序员的爱恨情仇
- day16:RDD实战(RDD基本操作实战及Transformation流程图)
- Java网络编程的基本使用
- UISegmentedControl 使用
- C# 中的委托和事件
- 《ASP.NET5》Windows 7下Visual Studio 2015安装记录