No loop; No recursion; 我没找到规律 但是从讨论区看到别人的思路:
If N
is a power of 3
:
- It follows that
3^X == N
- It follows that
log (3^X) == log N
- It follows that
X log 3 == log N
- It follows that
X == (log N) / (log 3)
- For the basis to hold,
X
must be an integer.
代码:
public class Solution { public boolean isPowerOfThree(int n) { double diff = 10e-15; double x = Math.log(n)/Math.log(3); return Math.abs( x - Math.round(x)) <= diff; } }