Given an integer, write a function to determine if it is a power of three.
Follow up:
Could you do it without using any loop / recursion?
解法一:
class Solution {
public:
bool isPowerOfThree(int n) {
double ans = log10(n)/log10(3);
return !(ans - (int)ans);
}
};
解法二:
class Solution {
public:
bool isPowerOfThree(int n) {
return (n>0) && !(1162261467%n);
}
};
解法三:
class Solution {
public:
bool isPowerOfThree(int n) {
return (n == 1 || n == 3 || n == 9 || n == 27 || n == 81 || n == 243 || n == 729 || n == 2187 || n == 6561 || n == 19683 || n == 59049 || n == 177147 || n == 531441 || n == 1594323 || n == 4782969 || n == 14348907 || n == 43046721 || n == 129140163 || n == 387420489 || n == 1162261467);
}
};
瞎了。。。