首页 > 代码库 > [LeetCode] Power of Three

[LeetCode] 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的幂,则这个数a的所有的约数都是3的幂,如果一个数b小于这个数a且是3的幂,则这个数b一定是a的约数。所以找出3的最大的幂,然后用这个数对n取余即可。

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

相关题目:Power of Two

相关题目:Power of Four

[LeetCode] Power of Three