231. Power of Two
2020-06-09 本文已影响0人
铭小狮子酱
思路
2
的倍数的二进制特点是首位为1
,则满足
注意最外层的括号不能省。
代码(cpp)
class Solution {
public:
bool isPowerOfTwo(int n) {
return n > 0 && (n & (n - 1)) == 0;
}
};
2
的倍数的二进制特点是首位为1
,则满足
注意最外层的括号不能省。
class Solution {
public:
bool isPowerOfTwo(int n) {
return n > 0 && (n & (n - 1)) == 0;
}
};