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,看最后能否得到1。

如果不用循环和递归,那么就需要找数学方法了。最简单的,我们可以把该整数对3取对数,如果结果是整数,说明该整数是3的幂。

代码

  1. // Power of Three
  2. // Time Complexity: O(1), Space Complexity: O(1)
  3. public class Solution {
  4. public boolean isPowerOfThree(int n) {
  5. return (Math.log10(n) / Math.log10(3)) % 1 == 0;
  6. }
  7. }

原文: https://soulmachine.gitbooks.io/algorithm-essentials/content/java/simulation/power-of-three.html