Lintcode142 O(1) Check Power Of 2 solution 题解
来源:互联网 发布:流金网络官网 编辑:程序博客网 时间:2024/04/29 06:14
【题目描述】
Using O(1) time to check whether an integernis a power of2.
用O(1) 时间检测整数n是否是2的幂次。
【题目链接】
www.lintcode.com/en/problem/o1-check-power-of-2/
【题目解析】
简单点来考虑可以连除2求余,看最后的余数是否为1,但是这种方法无法在 O(1) 的时间内解出,所以必须要想点别的办法。2的整数幂若用二进制来表示,则其 中必只有一个1,其余全是0,那么怎么才能用一个式子把这种特殊的关系表示出来了?传统的位运算如按位与、按位或和按位异或等均无法直接求解,比较下x - 1和x的关系试试.以x=4为例。
0100 ==> 4
0011 ==> 3
两个数进行按位与就为0了.如果不是2的整数幂则无上述关系,反证法可证之。
【参考答案】
www.jiuzhang.com/solutions/o1-check-power-of-2/
阅读全文
0 0
- Lintcode142 O(1) Check Power Of 2 solution 题解
- [刷题]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
- #142 O(1) Check Power of 2
- [Microsoft] O(1) Check Power of 2
- LintCode O(1) Check Power of 2 O(1)检测2的幂次
- 142.O(1) Check Power of 2-O(1)时间检测2的幂次(容易题)
- Lintcode140 Fast Power solution 题解
- check a int number is power of 2?
- LeetCode题解:Power of Two
- LeetCode题解:Power of Two
- LeetCode题解:Power of Three
- Lintcode14 First Position of Target Solution 题解
- Lintcode97 Maximum Depth Of BinaryTree solution 题解
- closest pair of points(O(nlogn)) solution
- LeetCode 题解(191) : Power of Two
- 破解SHA-1密码
- 浏览器访问网页的详细内部过程
- java中数组的基本操作
- python学习笔记2:print的使用中的一些问题
- Hbase(1)基础篇
- Lintcode142 O(1) Check Power Of 2 solution 题解
- 栈和队列
- 攻破 JAVA NIO 技术壁垒( 下 ) 2017-09-02 ImportNew (点击上方公众号,可快速关注) 来源:朱小厮, blog.csdn.net/u013256816/articl
- pyhton得unittest测试框架
- Python中列表的内置函数
- 【C++】初识多态!!!
- SpringMVC访问静态资源的三种方式
- Oracle笔记十: 约束
- newoj墓地2913: 马拦过河卒 knight