1.2的幂

正确写法:

class Solution {
public:
bool isPowerOfTwo(int n) {
if(n <= )
return false;
return (n & (n-)) == ;
}
};

错误写法1:

&符号的短路原则,如果&前面为false了就不会计算后面的了

class Solution {
public:
bool isPowerOfTwo(int n) {
if(n <= )
return false;
retur
class Solution {
public:
bool isPowerOfFour(int num) {
if(num <= )
return false;
if((num & (num - )) == ){
if(num & 0x55555555)
return true;
else
return false;
}
else
return false;
}
};
n ((n-) & n) == ;
}
};

错误写法2

==符号的优先级比&高

class Solution {
public:
bool isPowerOfTwo(int n) {
if(n <= )
return false;
return n & (n-) == ;
}
};

2.4的幂

class Solution {
public:
bool isPowerOfFour(int num) {
if(num <= )
return false;
if((num & (num - )) == ){
if(num & 0x55555555)
return true;
else
return false;
}
else
return false;
}
};

3.3的幂

https://blog.csdn.net/u014218090/article/details/80152446

05-26 05:25