题目描述:
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?
题目大意:给一个整数,判断它是否3的幂。
思路:直接做当然可以…可是没什么必要,还不如鸡贼一点,直接打表(2^32之内3的幂肯定不会超过30个)。
c++代码:
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);
}
};