LintCode-O(1) Check Power of 2
2014-12-27 05:13
375 查看
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
Analysis:
Use bit manipulation. Be carefull about the 0 and negtive integer.
Solution:
Example
For n=4, return true
For n=5, return false
Challenge
O(1) time
Analysis:
Use bit manipulation. Be carefull about the 0 and negtive integer.
Solution:
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; } };
相关文章推荐
- LintCode O(1) Check Power of 2 O(1)检测2的幂次
- lintcode-medium-Letter Combinations of a Phone Number
- Code Generate of Power Designer[转]
- Google/LintCode:H-Median of two Sorted Array
- Lintcode: Binary Tree Serialization (Serialization and Deserialization Of Binary Tree)
- lintcode:First Position of Target
- lintcode: O(1) Check Power of 2
- o1-check-power-of-2 && search-a-2d-matrix
- lintcode:First Position of Target
- Lintcode: O(1) Check Power of 2
- LintCode Delete Node in the Middle of Singly Linked List 在O(1)时间复杂度删除链表节点
- #142 O(1) Check Power of 2
- lintcode-medium-Number of Airplanes in the Sky
- [LeedCode OJ]#231 Power of Two
- *[Lintcode]O(1) Check Power of 2
- PowerTip of the Day-Check for a Battery
- 【LetCode——342】Power of four
- Ten Ways to Check if an Integer Is a Power Of Two in C
- lintcode 容易题:Delete Node in the Middle of Singly Linked List 在O(1)时间复杂度删除链表节点
- LintCode "Number of Islands II"