码迷,mamicode.com
首页 > 其他好文 > 详细

326 Power of Three 3的幂

时间:2018-04-14 16:51:27      阅读:587      评论:0      收藏:0      [点我收藏+]

标签:https   tps   blog   turn   log   des   return   leetcode   -o   

给出一个整数,写一个函数来确定这个数是不是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

326 Power of Three 3的幂

标签:https   tps   blog   turn   log   des   return   leetcode   -o   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!