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

posted on 2018-04-14 16:36  lina2014  阅读(120)  评论(0编辑  收藏  举报

导航