[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的多少次方?

3​​ =  N    =>   x = log3​N  =>   x = log10​N/log10​3 再判断X是否为正整数,如果是为true,否则为false。

code:

public class Solution {
    public boolean isPowerOfThree(int n) {
        
        if(n<=0 )
            return false;
        double result = Math.log10(n)/Math.log10(3);
        if(result %1 == 0)
        {
            return true;
        }
        return false;
    }
}

你可能感兴趣的:([LeetCode]Power of Three)