|
| 1 | +--- |
| 2 | +title: 326.3 的幂:大幂整除 |
| 3 | +date: 2025-08-13 13:26:49 |
| 4 | +tags: [题解, LeetCode, 简单, 递归, 数学] |
| 5 | +categories: [题解, LeetCode] |
| 6 | +--- |
| 7 | + |
| 8 | +# 【LetMeFly】326.3 的幂:大幂整除 |
| 9 | + |
| 10 | +力扣题目链接:[https://leetcode.cn/problems/power-of-three/](https://leetcode.cn/problems/power-of-three/) |
| 11 | + |
| 12 | +<p>给定一个整数,写一个函数来判断它是否是 3 的幂次方。如果是,返回 <code>true</code> ;否则,返回 <code>false</code> 。</p> |
| 13 | + |
| 14 | +<p>整数 <code>n</code> 是 3 的幂次方需满足:存在整数 <code>x</code> 使得 <code>n == 3<sup>x</sup></code></p> |
| 15 | + |
| 16 | +<p> </p> |
| 17 | + |
| 18 | +<p><strong>示例 1:</strong></p> |
| 19 | + |
| 20 | +<pre> |
| 21 | +<strong>输入:</strong>n = 27 |
| 22 | +<strong>输出:</strong>true |
| 23 | +</pre> |
| 24 | + |
| 25 | +<p><strong>示例 2:</strong></p> |
| 26 | + |
| 27 | +<pre> |
| 28 | +<strong>输入:</strong>n = 0 |
| 29 | +<strong>输出:</strong>false |
| 30 | +</pre> |
| 31 | + |
| 32 | +<p><strong>示例 3:</strong></p> |
| 33 | + |
| 34 | +<pre> |
| 35 | +<strong>输入:</strong>n = 9 |
| 36 | +<strong>输出:</strong>true |
| 37 | +</pre> |
| 38 | + |
| 39 | +<p><strong>示例 4:</strong></p> |
| 40 | + |
| 41 | +<pre> |
| 42 | +<strong>输入:</strong>n = 45 |
| 43 | +<strong>输出:</strong>false |
| 44 | +</pre> |
| 45 | + |
| 46 | +<p> </p> |
| 47 | + |
| 48 | +<p><strong>提示:</strong></p> |
| 49 | + |
| 50 | +<ul> |
| 51 | + <li><code>-2<sup>31</sup> <= n <= 2<sup>31</sup> - 1</code></li> |
| 52 | +</ul> |
| 53 | + |
| 54 | +<p> </p> |
| 55 | + |
| 56 | +<p><strong>进阶:</strong>你能不使用循环或者递归来完成本题吗?</p> |
| 57 | + |
| 58 | + |
| 59 | + |
| 60 | +## 解题方法:看n能否被“最大的”3的幂整除 |
| 61 | + |
| 62 | +题目有一行进阶小字说“你能不使用循环或者递归来完成本题吗?”,好一个挑衅(provocation,bushi),那就不递归或者循环了吧。 |
| 63 | + |
| 64 | +类似[【LetMeFly】231.2 的幂:五种小方法判断](https://blog.letmefly.xyz/2022/09/08/LeetCode%200231.2%E7%9A%84%E5%B9%82/)的方法五,我们直接使用题目数据范围内最大的3的幂对n取模,看余数是否为0就好了。 |
| 65 | + |
| 66 | +如何求题目数据范围内最大的3的幂?打开python有: |
| 67 | + |
| 68 | +```python |
| 69 | +>>> 3**20 > 2**31-1 |
| 70 | +True |
| 71 | +>>> 3**19 > 2**31-1 |
| 72 | +False |
| 73 | +>>> 3**19 |
| 74 | +1162261467 |
| 75 | +``` |
| 76 | + |
| 77 | +可知如果不大于$2^{31}-1$的正整数$n$是$3$的幂那么它一定能够被$1162261467$整除。 |
| 78 | + |
| 79 | ++ 时间复杂度$O(1)$ |
| 80 | ++ 空间复杂度$O(1)$ |
| 81 | + |
| 82 | +### AC代码 |
| 83 | + |
| 84 | +#### C++ |
| 85 | + |
| 86 | +```cpp |
| 87 | +/* |
| 88 | + * @Author: LetMeFly |
| 89 | + * @Date: 2025-08-13 13:17:09 |
| 90 | + * @LastEditors: LetMeFly.xyz |
| 91 | + * @LastEditTime: 2025-08-13 13:25:31 |
| 92 | + */ |
| 93 | +class Solution { |
| 94 | +public: |
| 95 | + bool isPowerOfThree(int n) { |
| 96 | + return n > 0 && !(1162261467 % n); |
| 97 | + } |
| 98 | +}; |
| 99 | +``` |
| 100 | +
|
| 101 | +#### Python |
| 102 | +
|
| 103 | +```python |
| 104 | +''' |
| 105 | +Author: LetMeFly |
| 106 | +Date: 2025-08-13 13:17:09 |
| 107 | +LastEditors: LetMeFly.xyz |
| 108 | +LastEditTime: 2025-08-13 13:24:54 |
| 109 | +''' |
| 110 | +class Solution: |
| 111 | + def isPowerOfThree(self, n: int) -> bool: |
| 112 | + return n > 0 and not 1162261467 % n |
| 113 | +``` |
| 114 | + |
| 115 | +#### Java |
| 116 | + |
| 117 | +```java |
| 118 | +/* |
| 119 | + * @Author: LetMeFly |
| 120 | + * @Date: 2025-08-13 13:17:09 |
| 121 | + * @LastEditors: LetMeFly.xyz |
| 122 | + * @LastEditTime: 2025-08-13 13:23:25 |
| 123 | + */ |
| 124 | +class Solution { |
| 125 | + public boolean isPowerOfThree(int n) { |
| 126 | + return n > 0 && 1162261467 % n == 0; |
| 127 | + } |
| 128 | +} |
| 129 | +``` |
| 130 | + |
| 131 | +#### Go |
| 132 | + |
| 133 | +```go |
| 134 | +/* |
| 135 | + * @Author: LetMeFly |
| 136 | + * @Date: 2025-08-13 13:17:09 |
| 137 | + * @LastEditors: LetMeFly.xyz |
| 138 | + * @LastEditTime: 2025-08-13 13:23:55 |
| 139 | + */ |
| 140 | +package main |
| 141 | + |
| 142 | +func isPowerOfThree(n int) bool { |
| 143 | + return n > 0 && 1162261467 % n == 0 |
| 144 | +} |
| 145 | +``` |
| 146 | + |
| 147 | +#### Rust |
| 148 | + |
| 149 | +```rust |
| 150 | +/* |
| 151 | + * @Author: LetMeFly |
| 152 | + * @Date: 2025-08-13 13:17:09 |
| 153 | + * @LastEditors: LetMeFly.xyz |
| 154 | + * @LastEditTime: 2025-08-13 13:21:37 |
| 155 | + */ |
| 156 | +impl Solution { |
| 157 | + pub fn is_power_of_three(n: i32) -> bool { |
| 158 | + n > 0 && 1162261467 % n == 0 |
| 159 | + } |
| 160 | +} |
| 161 | +``` |
| 162 | + |
| 163 | +> 同步发文于[CSDN](https://letmefly.blog.csdn.net/article/details/150343626)和我的[个人博客](https://blog.letmefly.xyz/),原创不易,转载经作者同意后请附上[原文链接](https://blog.letmefly.xyz/2025/08/13/LeetCode%200326.3%E7%9A%84%E5%B9%82/)哦~ |
| 164 | +> |
| 165 | +> 千篇源码题解[已开源](https://github.com/LetMeFly666/LeetCode) |
0 commit comments