326. Power of Three
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的幂
思路1,迭代,不断除3,能否除尽
思路2,log3,若是3的幂则结果是整数
思路3,找int可以定义的最大的那个3次幂,除以3,能整除则是,否则不是
最大的数 2^32/2-1,为3^19次方
class Solution {
public:
bool isPowerOfThree(int n) {
double ans=log10(n)/log10(3);
return (ans-int(ans)==0)?true:false;
}
};