1. 程式人生 > >[LeetCode] Power of Three

[LeetCode] Power of Three

solution html color mine 如果 do it href with col

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