Given an integer, write a function to determine if it is a power of two.
solution:
用二进制的特点
code:
class Solution {
public:
bool isPowerOfTwo(int n) {
return n > 0 && !(n&(n-1));
}
};
Given an integer, write a function to determine if it is a power of two.
solution:
用二进制的特点
code:
class Solution {
public:
bool isPowerOfTwo(int n) {
return n > 0 && !(n&(n-1));
}
};