326. Power of Three
递归版本:
class Solution {
public:
bool isPowerOfThree(int n) {
if(n <= 0)
return false;
if(n == 1)
return true;
else if(n%3 == 0)
isPowerOfThree(n/3);
else
return false;
}
};
不使用递归或迭代的版本,来自StackOverflow
class Solution {
public:
bool isPowerOfThree(int n) {
return (n > 0 && (1162261467)%n==0);
}
};