Leetcode 326 - Power of Three

Posted on February 28, 2016

Last updated on March 2, 2016

Problem link. This is simple, just make sure you don’t overflow (I do it with the q > 0 check).

public boolean isPowerOfThree(int n) {
  int q = 1;
  for(int i = 1; (q > 0 && q <= n); i++) {
    if (q == n) return true;
    q *= 3;
  }
  return false;
}
Markdown SHA1: f4e05e8562a4f880d78aedc1e313a622f69f546b