> For the complete documentation index, see [llms.txt](https://protegejj.gitbook.io/algorithm-practice/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://protegejj.gitbook.io/algorithm-practice/google/326-power-of-three.md).

# 326 Power of Three

## 326. [Power of Three](https://leetcode.com/problems/power-of-three/description/)

## 1. Question

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?

## 2. Implementation

```java
class Solution {
    public boolean isPowerOfThree(int n) {
        while (n > 0 && n % 3 == 0) {
            n = n/3;
        }
        return n == 1;
    }
}
```

## 3. Time & Space Complexity

时间复杂度O(logn), 空间复杂度O(1)
