LeetCode - 326. Power of Three

链接

326. Power of Three

题意

3的幂。
给定一个整数,判断其是否为3的幂。

思路

较好的办法是计算出整型范围内3的幂的最大值,用这个值来模3,若等于0则成立。

代码

public class Solution {
    public boolean isPowerOfThree(int n) {
        // 1162261467 is 3^19,  3^20 is bigger than int  
        return ( n>0 &&  1162261467%n==0);
    }
}
posted @ 2017-07-14 15:14  zyoung  阅读(138)  评论(0编辑  收藏  举报