#142 O(1) Check Power of 2
来源:互联网 发布:linux oracle多实例 编辑:程序博客网 时间:2024/05/14 08:59
题目描述:
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
每个int32都一共只有32位,而如果是2的power,那么这32位中必须只有一位是‘1’。
Mycode(AC = 39ms):
class Solution {public: /* * @param n: An integer * @return: True or false */ bool checkPowerOf2(int n) { // write your code here long long ln = (long long)n; if (ln < 0) return false; // traverse all the 32 bits, the result // should only contain one '1' int count = 0; for (int i = 0; i < 32; i++) { count += ln % 2; ln >>= 1; } return count == 1; }};
0 0
- #142 O(1) Check Power of 2
- [刷题]O(1) Check Power of 2
- O(1) Check Power of 2
- lintcode: O(1) Check Power of 2
- *[Lintcode]O(1) Check Power of 2
- [Microsoft] O(1) Check Power of 2
- Lintcode142 O(1) Check Power Of 2 solution 题解
- LintCode O(1) Check Power of 2 O(1)检测2的幂次
- 142.O(1) Check Power of 2-O(1)时间检测2的幂次(容易题)
- check a int number is power of 2?
- Next Power of 2
- 1 Power of Cryptography
- Leetcode 231 and 342. Power of 2, Power of 4
- power of 2 or not
- 326. Power of Three#1
- Ten Ways to Check if an Integer Is a Power Of Two in C
- Ten Ways to Check if an Integer Is a Power Of Two in C
- Ten Ways to Check if an Integer Is a Power Of Two in C
- LeetCode—386. Lexicographical Numbers
- Android中Canvas绘图基础详解
- Android Drawable详解
- .net下的面向工控领域的远程方法调用(RMI)中间件,客户端协议栈请求端实现
- ios代理模式深入了解一下
- #142 O(1) Check Power of 2
- Oracle数据库-4性能优化
- access时分秒
- JavaScript全局函数与自定义函数-Functions
- 卡方检验原理及应用
- A20 Plus(esp8266与A6C)拍照传输图片
- 关于android中的drawable中seletor的使用
- 笔记--ThinkPHP模板判断之if elseif else
- 离职京东的自我总结