思路
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;
}
};
本文标题:231. Power of Two
本文链接:https://www.haomeiwen.com/subject/aymjtktx.html
网友评论