326. Power of Three
logn3=logn10log310
class Solution {
public:
bool isPowerOfThree(int n) {
double logRes = log10(n) / log10(3);
if (logRes - (int)logRes == 0){
return true;
}
return false;
}
};
231. Power of Two
class Solution {
public:
bool isPowerOfTwo(int n) {
if(n<=0){
return false;
}
return (n&(n-1))==0;
}
};
一定要注意
if(n<=0){
return false;
}
342. Power of Four
class Solution {
public:
bool isPowerOfFour(int n) {
if (n <= 0){
return false;
}
return (n&(n - 1)) == 0 && (n & 0x55555555)==n;
}
};
一方面是2的次幂,但是还有另外的特征,4的次幂中
1处于奇数位上