LeetCode - Algorithms - 326. Power of Three

Java

1 Integer.Max_Value

1
2
3
4
5
class Solution {
public boolean isPowerOfThree(int n) {
return n>0 && 1162261467%n==0?true:false;
}
}

Submission Detail

  • Runtime: 12 ms, faster than 99.94% of Java online submissions for Power of Three.
  • Memory Usage: 38.1 MB, less than 0.99% of Java online submissions for Power of Three.
  • 21038 / 21038 test cases passed.

2 Logarithm

1
2
3
4
5
6
7
class Solution {
public boolean isPowerOfThree(int n) {
if (n<=0) return false;
double power = Math.log10(n)/Math.log10(3);
return power==Math.ceil(power)?true:false;
}
}

Submission Detail

  • Runtime: 15 ms, faster than 52.50% of Java online submissions for Power of Three.
  • Memory Usage: 37.3 MB, less than 0.99% of Java online submissions for Power of Three.
  • 21038 / 21038 test cases passed.