2022-03-27 18:35:17 +08:00
|
|
|
<p>Given an integer <code>n</code>, return <em><code>true</code> if it is a power of three. Otherwise, return <code>false</code></em>.</p>
|
|
|
|
|
|
|
|
<p>An integer <code>n</code> is a power of three, if there exists an integer <code>x</code> such that <code>n == 3<sup>x</sup></code>.</p>
|
|
|
|
|
|
|
|
<p> </p>
|
2023-12-09 18:42:21 +08:00
|
|
|
<p><strong class="example">Example 1:</strong></p>
|
2022-03-27 18:35:17 +08:00
|
|
|
|
|
|
|
<pre>
|
|
|
|
<strong>Input:</strong> n = 27
|
|
|
|
<strong>Output:</strong> true
|
2023-12-09 18:42:21 +08:00
|
|
|
<strong>Explanation:</strong> 27 = 3<sup>3</sup>
|
2022-03-27 18:35:17 +08:00
|
|
|
</pre>
|
|
|
|
|
2023-12-09 18:42:21 +08:00
|
|
|
<p><strong class="example">Example 2:</strong></p>
|
2022-03-27 18:35:17 +08:00
|
|
|
|
|
|
|
<pre>
|
|
|
|
<strong>Input:</strong> n = 0
|
|
|
|
<strong>Output:</strong> false
|
2023-12-09 18:42:21 +08:00
|
|
|
<strong>Explanation:</strong> There is no x where 3<sup>x</sup> = 0.
|
2022-03-27 18:35:17 +08:00
|
|
|
</pre>
|
|
|
|
|
2023-12-09 18:42:21 +08:00
|
|
|
<p><strong class="example">Example 3:</strong></p>
|
2022-03-27 18:35:17 +08:00
|
|
|
|
|
|
|
<pre>
|
2023-12-09 18:42:21 +08:00
|
|
|
<strong>Input:</strong> n = -1
|
|
|
|
<strong>Output:</strong> false
|
|
|
|
<strong>Explanation:</strong> There is no x where 3<sup>x</sup> = (-1).
|
2022-03-27 18:35:17 +08:00
|
|
|
</pre>
|
|
|
|
|
|
|
|
<p> </p>
|
|
|
|
<p><strong>Constraints:</strong></p>
|
|
|
|
|
|
|
|
<ul>
|
|
|
|
<li><code>-2<sup>31</sup> <= n <= 2<sup>31</sup> - 1</code></li>
|
|
|
|
</ul>
|
|
|
|
|
|
|
|
<p> </p>
|
|
|
|
<strong>Follow up:</strong> Could you solve it without loops/recursion?
|