标签:int challenge https desc dde check you code topic
Topic: Math&Bit Manipulation
Using O(1) time to check whether an integer n is a power of 2.
Example
Example 1:
Input: 4
Output: true
Example 2:
Input: 5
Output: false
Challenge
O(1) time
class Solution {
/*
* @param n: An integer
* @return: True or false
*/
public boolean checkPowerOf2(int n) {
// write your code here
return n > 0 && (n & (n - 1)) == 0;
}
}
没想到合适的方法。不太会做这类的题
[Lintcode]142. O(1) Check Power of 2
标签:int challenge https desc dde check you code topic
原文地址:https://www.cnblogs.com/siriusli/p/10358610.html