326 Power of Three 3的幂

给出一个整数,写一个函数来确定这个数是不是3的一个幂。
后续挑战:
你能不使用循环或者递归完成本题吗?

详见:https://leetcode.com/problems/power-of-three/description/

C++:

方法一:

class Solution {
public:
    bool isPowerOfThree(int n) {
        while(n&&n%3==0)
        {
            n/=3;
        }
        return n==1;
    }
};

 方法二:

class Solution {
public:
    bool isPowerOfThree(int n) {
        return (n>0&&1162261467%n==0);
    }
};

 参考:https://www.cnblogs.com/grandyang/p/5138212.html

原文地址:https://www.cnblogs.com/xidian2014/p/8832583.html