Skip to content

Commit 502d623

Browse files
committed
update: 添加问题“326.3的幂”的代码和题解 (#1079)
326: AC.rust+java+go+py+cpp (#1078) rust - AC,51.11%,100.00% java - AC,89.67%,5.37% go - AC,20.00%,41.05% py - AC,46.89%,5.52% cpp - AC,25.79%,19.01% 今天(相比于之前)倒着写的 Signed-off-by: LetMeFly666 <[email protected]>
1 parent 3ae2f9c commit 502d623

10 files changed

+233
-2
lines changed

.commitmsg

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,8 @@
1-
close #1074
1+
326: AC.rust+java+go+py+cpp (#1078)
2+
3+
rust - AC,51.11%,100.00%
4+
java - AC,89.67%,5.37%
5+
go - AC,20.00%,41.05%
6+
py - AC,46.89%,5.52%
7+
cpp - AC,25.79%,19.01%
8+
今天(相比于之前)倒着写的
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-08-13 13:17:09
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-08-13 13:25:31
6+
*/
7+
class Solution {
8+
public:
9+
bool isPowerOfThree(int n) {
10+
return n > 0 && !(1162261467 % n);
11+
}
12+
};
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-08-13 13:17:09
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-08-13 13:23:55
6+
*/
7+
package main
8+
9+
func isPowerOfThree(n int) bool {
10+
return n > 0 && 1162261467 % n == 0
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-08-13 13:17:09
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-08-13 13:23:25
6+
*/
7+
class Solution {
8+
public boolean isPowerOfThree(int n) {
9+
return n > 0 && 1162261467 % n == 0;
10+
}
11+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
'''
2+
Author: LetMeFly
3+
Date: 2025-08-13 13:17:09
4+
LastEditors: LetMeFly.xyz
5+
LastEditTime: 2025-08-13 13:24:54
6+
'''
7+
class Solution:
8+
def isPowerOfThree(self, n: int) -> bool:
9+
return n > 0 and not 1162261467 % n
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-08-13 13:17:09
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-08-13 13:21:37
6+
*/
7+
impl Solution {
8+
pub fn is_power_of_three(n: i32) -> bool {
9+
n > 0 && 1162261467 % n == 0
10+
}
11+
}

Codes/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@
55
* @LastEditTime: 2025-08-11 22:07:08
66
*/
77
pub struct Solution;
8-
include!("2787-ways-to-express-an-integer-as-sum-of-powers.rs"); // 这个fileName是会被脚本替换掉的
8+
include!("0326-power-of-three_20250813.rs"); // 这个fileName是会被脚本替换掉的

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,7 @@
275275
|0316.去除重复字母|中等|<a href="https://leetcode.cn/problems/remove-duplicate-letters/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2022/09/23/LeetCode%200316.%E5%8E%BB%E9%99%A4%E9%87%8D%E5%A4%8D%E5%AD%97%E6%AF%8D/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/127011739" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/remove-duplicate-letters/solution/letmefly-316qu-chu-zhong-fu-zi-mu-dan-di-tczt/" target="_blank">LeetCode题解</a>|
276276
|0318.最大单词长度乘积|中等|<a href="https://leetcode.cn/problems/maximum-product-of-word-lengths/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/11/06/LeetCode%200318.%E6%9C%80%E5%A4%A7%E5%8D%95%E8%AF%8D%E9%95%BF%E5%BA%A6%E4%B9%98%E7%A7%AF/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/134254146" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/maximum-product-of-word-lengths/solutions/2515432/letmefly-318zui-da-dan-ci-chang-du-cheng-ytzs/" target="_blank">LeetCode题解</a>|
277277
|0322.零钱兑换|中等|<a href="https://leetcode.cn/problems/coin-change/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/03/24/LeetCode%200322.%E9%9B%B6%E9%92%B1%E5%85%91%E6%8D%A2/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/136985285" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/coin-change/solutions/2704784/letmefly-322ling-qian-dui-huan-dong-tai-db3o0/" target="_blank">LeetCode题解</a>|
278+
|0326.3的幂|简单|<a href="https://leetcode.cn/problems/power-of-three/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/08/13/LeetCode%200326.3%E7%9A%84%E5%B9%82/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/150343626" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/power-of-three/solutions/3751661/letmefly-3263-de-mi-da-mi-zheng-chu-by-t-9hdc/" target="_blank">LeetCode题解</a>|
278279
|0328.奇偶链表|中等|<a href="https://leetcode.cn/problems/odd-even-linked-list/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2022/09/24/LeetCode%200328.%E5%A5%87%E5%81%B6%E9%93%BE%E8%A1%A8/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/127020912" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/odd-even-linked-list/solution/letmefly-328qi-ou-lian-biao-by-tisfy-x9d8/" target="_blank">LeetCode题解</a>|
279280
|0329.矩阵中的最长递增路径|困难|<a href="https://leetcode.cn/problems/longest-increasing-path-in-a-matrix/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2022/09/25/LeetCode%200329.%E7%9F%A9%E9%98%B5%E4%B8%AD%E7%9A%84%E6%9C%80%E9%95%BF%E9%80%92%E5%A2%9E%E8%B7%AF%E5%BE%84/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/127043063" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/longest-increasing-path-in-a-matrix/solution/letmefly-329ju-zhen-zhong-de-zui-chang-d-4j3g/" target="_blank">LeetCode题解</a>|
280281
|0337.打家劫舍III|中等|<a href="https://leetcode.cn/problems/house-robber-iii/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2022/09/27/LeetCode%200337.%E6%89%93%E5%AE%B6%E5%8A%AB%E8%88%8DIII/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/127065525" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/house-robber-iii/solution/letmefly-337da-jia-jie-she-iii-by-tisfy-v7ao/" target="_blank">LeetCode题解</a>|

Solutions/LeetCode 0326.3的幂.md

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
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&nbsp;的幂次方。如果是,返回 <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>&nbsp;</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>&nbsp;</p>
47+
48+
<p><strong>提示:</strong></p>
49+
50+
<ul>
51+
<li><code>-2<sup>31</sup> &lt;= n &lt;= 2<sup>31</sup> - 1</code></li>
52+
</ul>
53+
54+
<p>&nbsp;</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)

Solutions/Other-English-LearningNotes-SomeWords.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1354,6 +1354,10 @@ categories: [自用]
13541354
|shrewd|adj. 机灵的,精明的|
13551355
|||
13561356
|prudent|adj. 谨慎的,精打细算的|
1357+
|||
1358+
|provocation|n. 挑衅,刺激,激怒|
1359+
|dignity|n. 尊重,高贵,庄严,端庄,尊严|
1360+
|commonsense|n. 常识,直觉判断力<br/>adj. 常识的,具有常识的|
13571361

13581362
+ 这个web要是能设计得可以闭眼(完全不睁眼)键盘控制背单词就好了。
13591363
+ 也许可以加个AI用最近词编故事功能(返回接口中支持标注所使用单词高亮?)

0 commit comments

Comments
 (0)