题目

power-of-three


2. 算法

* 直接模拟

* 直接模拟优化


3. 代码

* 直接模拟

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);
    }
};

results matching ""

    No results matching ""