# LeetCode 326.Power of Three is simple

This article belongs to "Conquer LeetCode"One of the series, which began officially in 2021/08/12. Because some of the titles on LeetCode are locked, this series will last at least until the day all the unlocked titles are brushed off. Because LeetCode is still creating new titles, the end date of this series may be forever. In this series of brushing titles, I will not only explain a variety of ideas and their optimization, but will also use a variety of optionsThe programming language implements the puzzle and summarizes the corresponding algorithm templates when it comes to general solutions.

To facilitate running debugging on PC s and sharing code files, I also set up a warehouse: https://github.com/memcpy0/LeetCode-Conquest In this warehouse, you can not only see the LeetCode topic links, problem code, problem article links, similar topic summaries, general solution summaries, etc., but also see important information such as the frequency of the original topic and related enterprises. If you have other preferred solutions, you can also share them with others.

As the content of this series of articles may change at any time, you are welcome to follow and collect them Conquer LeetCode Series Articles Directory One article for memo.

Given an integer n, return true if it is a power of three. Otherwise, return false.

An integer n is a power of three, if there exists an integer x such that n == 3x.

Example 1:

```Input: n = 27
Output: true
```

Example 2:

```Input: n = 0
Output: false
```

Example 3:

```Input: n = 9
Output: true
```

Example 4:

```Input: n = 45
Output: false
```

Constraints:

• -231 <= n <= 231 - 1

Follow up: Could you solve it without loops/recursion?

Topic: Given an integer, write a function to determine if it is a power of 3. If it is, return true; otherwise, return false. Advanced does this without loops or recursion.

This question is similar to 231. Power of Two and 342. Power of Four But the power of 2 and the power of 4 are more similar.

### Solution 1 Iteration+Trial

The simple solution is to keep adding n n n pairs 3 3 3 Trial until n n n is no longer associated with 3 3 3 is multiplied, then judge n n Is n equal to 3 0 = 1 3^0 = 1 30 = 1. Note that nonpositive integers are not 3 3 The time complexity of the algorithm is O ( log ⁡ 3 n ) O(\log_3 n) O(log3 n), spatial complexity is O ( 1 ) O(1) O(1) :

```//C++ version
class Solution {
public:
bool isPowerOfThree(int n) {
if (n <= 0) return false;
while (n % 3 == 0) n /= 3;
return n == 1;
}
};
//Execution time: 8 ms, defeated 96.39% of all C++ submissions
//Memory consumption: 5.6 MB, beating 94.77% of all C++ submissions
```

I am here Power of 4 The sx method in can also be used here. n n n to a floating point number and divide by 3 3 3, and finally decide if it's equal to 1.0 1.0 1.0 (not recommended):

```//C++ version
class Solution {
public:
bool isPowerOfThree(int n) {
double tn = n;
while (tn > 1.0) tn /= 3;
return tn == 1.0;
}
};
//Execution time: 16 ms, beat 71.42% of all C++ submissions
//Memory consumption: 5.9 MB, beating 40.15% of all C++ submissions
```

### Solution 2 Math Library Functions + Processing Errors (not recommended)

Use the math library functions log(), round(), but first determine if n is a positive integer and handle the error (probably the test case ratio) 231. Power of Two and 342. Power of Four Much more, over 20,000):

```//C++ version
class Solution {
public:
bool isPowerOfThree(int n) {
if (n <= 0) return false;
double k = log(n) / log(3);
return fabs(k - round(k)) <= 1e-10;
}
};
//Execution time: 8 ms, defeated 96.39% of all C++ submissions
//Memory consumption: 6 MB, beating 6.12% of all C++ submissions
```

### Solution 3 Tables

One of the easiest ways to think of "do not use loops/recursions" is to do table preprocessing. The time complexity of the algorithm is O ( 1 ) O(1) O(1), spatial complexity is O ( 1 ) O(1) O(1) :

```//C++ version
int ans = {1,3,9,27,81,243,729,2187,6561,19683,59049,177147,531441,1594323,4782969,14348907,43046721,129140163,387420489,1162261467};
class Solution {
public:
bool isPowerOfThree(int n) {
if (n <= 0) return false;
for (int i = 0; i < 20; ++i) if (n == ans[i]) return true;
return false;
}
};
//Execution time: 16 ms, beat 71.42% of all C++ submissions
//Memory consumption: 5.6 MB, beating 94.77% of all C++ submissions
```

### Solution 4 Mathematics (multiples/approximations)

n n The data type of n is int. As you can see from the table, the maximum third power in the range of int is 1162261467 1162261467 1162261467. If n n n is 3 3 The power of 3 must be satisfied n ∗ 3 k = 1162261467 n * 3^k = 1162261467 n_3k=1162261467, i.e. n n n and 1162261467 1162261467 1162261467 has a multiplier relationship. So you just need to decide n n n is a positive integer and is 1162261467 1162261467 The time complexity of the algorithm is O ( 1 ) O(1) O(1), spatial complexity is O ( 1 ) O(1) O(1) .

It is important to note that this is not a quick judgement x x General practice for the power of x if and only if x x x is a prime number, so it cannot be generalized to 342. Power of Four A simple example is, 64 64 64 is 4 4 The power of 4 must also be some of the largest 4 4 Power of 4 z z The approximate number of z, however 32 32 32 is not 4 4 The power of 4, but it must be 64 64 64, thus z z The approximate number of z.

```//C++ version
class Solution {
public:
bool isPowerOfThree(int n) {
return n > 0 && 1162261467 % n == 0;
}
};
//Execution time: 16 ms, beat 71.42% of all C++ submissions
//Memory consumption: 5.8 MB, beating 58.63% of all C++ submissions
```
1. Power of 3
https://leetcode-cn.com/problems/power-of-three/

Posted on Tue, 28 Sep 2021 12:13:16 -0400 by shakuni