LeetCode 326: Power of Three

MathRecursion

Problem Description

Explanation

To determine if a given number is a power of three, we can repeatedly divide the number by 3 until we reach 1. If at any point the number is not divisible by 3 or if we reach 1, then the original number is not a power of three. The constraints allow us to use a mathematical approach without loops or recursion.

Solutions

public boolean isPowerOfThree(int n) {
    return n > 0 && 1162261467 % n == 0;
}

Loading editor...