版权声明:博客文章都是作者辛苦整理的,转载请注明出处,谢谢! https://cloud.tencent.com/developer/article/1433646
文章作者:Tyan
博客:noahsnail.com | CSDN | 简书
class Solution {
public:
bool isPowerOfTwo(int n) {
if(n == 0) {
return false;
}
while(n != 1) {
if(n % 2) {
return false;
}
n /= 2;
}
return true;
}
};
class Solution {
public:
bool isPowerOfTwo(int n) {
if(n == 0) {
return false;
}
while(n != 1) {
if(n % 2) {
return false;
}
n >>= 1;
}
return true;
}
};
class Solution {
public:
bool isPowerOfTwo(int n) {
return n > 0 && !(n & (n - 1));
}
};