class Solution {
public:
bool isPowerOfTwo(int n) {
return n > 0 && (n & (n-1))==0;
}
};
n&n-1==0 时 n为2的倍数
class Solution {
public:
bool isPowerOfTwo(int n) {
return n > 0 && (n & -n) == n;
}
};
n & -n == n 也行n为2的倍数
class Solution {
public:
bool isPowerOfThree(int n) {
while(n&&n%3==0){
n/=3;
}
return n==1;
}
};
class Solution {
public:
bool isPowerOfThree(int n) {
if(n <= 0) {
return false;
}
int x = (int)(log2(n) / log2(3) + 1e-8);
return fabs(n - pow(3, x)) < 1e-8;
}
};
求1+2+....+n
class Solution {
public:
int res;
int sumNums(int n) {
bool x = n > 1 && sumNums(n - 1) > 0;
res += n;
return res;
}
};