Skip to content

Commit c2d75e2

Browse files
authored
update: 添加问题“342.4的幂”的代码(并更新其题解) (#1083)
* 342: RE(+WA).cpp (#1082) * 342: AC.cpp (#1082) cpp - AC,100.00%,82.09% * 342: AC.cpp (#1082) - mod 3 cpp - AC,100.00%,33.86% * 342: py.cpp (#1082) - mod 3 py - AC,12.55%,65.26% * 342: AC.go+WA.java (#1082) - mod 3 go - AC,100.00%,42.17% * 342: AC.java (#1082) - mod 3 java - AC,100.00%,40.06% * 342: AC.rust (#1082) - mod 3 rust - AC,100.00%,33.33% * update: 添加问题“342.4的幂”的代码(并更新其题解) (#1083) 326: AC.rust+java+go+py+cpp (#1078) cpp - AC,100.00%,43.53% py - AC,100.00%,59.18% go - AC,100.00%,90.00% java - AC,100.00%,75.56% rust - AC,100.00%,-% # 是的,没看错 Signed-off-by: LetMeFly666 <[email protected]> --------- Signed-off-by: LetMeFly666 <[email protected]>
1 parent c49f44b commit c2d75e2

9 files changed

+189
-4
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-08-15 18:29:00
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-08-15 18:50:45
6+
*/
7+
#if defined(_WIN32) || defined(__APPLE__)
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
class Solution {
12+
public:
13+
bool isPowerOfFour(int n) {
14+
if (n <= 0) {
15+
return false;
16+
}
17+
int k = sqrt(n);
18+
return k * k == n && (n & (n - 1)) == 0;
19+
}
20+
};
21+
22+
#if defined(_WIN32) || defined(__APPLE__)
23+
/*
24+
16
25+
26+
*/
27+
int main() {
28+
int n;
29+
while (cin >> n) {
30+
Solution sol;
31+
cout << sol.isPowerOfFour(n) << endl;
32+
}
33+
return 0;
34+
}
35+
#endif
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-08-15 18:29:00
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-08-15 18:49:20
6+
*/
7+
#if defined(_WIN32) || defined(__APPLE__)
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
class Solution {
12+
public:
13+
bool isPowerOfFour(int n) {
14+
return n > 0 && (n & (n - 1)) == 0 && n % 3 == 1;
15+
}
16+
};
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-15 18:29:00
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-08-15 23:54:26
6+
*/
7+
package main
8+
9+
func isPowerOfFour(n int) bool {
10+
return n > 0 && n & (n - 1) == 0 && n % 3 == 1
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-15 18:29:00
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-08-15 23:56:02
6+
*/
7+
class Solution {
8+
public boolean isPowerOfFour(int n) {
9+
return n > 0 && (n & (n - 1)) == 0 && n % 3 == 1;
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-15 18:29:00
4+
LastEditors: LetMeFly.xyz
5+
LastEditTime: 2025-08-15 18:52:04
6+
'''
7+
class Solution:
8+
def isPowerOfFour(self, n: int) -> bool:
9+
return n > 0 and n & (n - 1) == 0 and n % 3 == 1
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-15 18:29:00
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-08-15 23:57:22
6+
*/
7+
impl Solution {
8+
pub fn is_power_of_four(n: i32) -> bool {
9+
n > 0 && n & (n - 1) == 0 && n % 3 == 1
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!("1780-check-if-number-is-a-sum-of-powers-of-three_20250814.rs"); // 这个fileName是会被脚本替换掉的
8+
include!("0342-power-of-four_20250815.rs"); // 这个fileName是会被脚本替换掉的

Solutions/LeetCode 0342.4的幂.md

Lines changed: 91 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,5 +81,94 @@ public:
8181
};
8282
```
8383
84-
> 同步发文于CSDN,原创不易,转载请附上[原文链接](https://blog.letmefly.xyz/2022/09/29/LeetCode%200342.4%E7%9A%84%E5%B9%82/)哦~
85-
> Tisfy:[https://letmefly.blog.csdn.net/article/details/127104821](https://letmefly.blog.csdn.net/article/details/127104821)
84+
## 方法二:判断是否 是2的幂且是平方数
85+
86+
如何判断一个数是否是2的幂?这里有5种方法判断:[【LetMeFly】231.2 的幂:五种小方法判断](http://blog.letmefly.xyz/2022/09/08/LeetCode%200231.2%E7%9A%84%E5%B9%82/)
87+
88+
如果一个数是2的幂,那么它是4的幂吗?只需要看这个数是否是平方数即可($\lfloor\sqrt{n}\rfloor^2==n$)。
89+
90+
这是因为$4^t=(2^{\frac{t}{2}})^2$。
91+
92+
+ 时间复杂度$O(1)$
93+
+ 空间复杂度$O(1)$
94+
95+
### AC代码
96+
97+
#### C++
98+
99+
```cpp
100+
class Solution {
101+
public:
102+
bool isPowerOfFour(int n) {
103+
if (n <= 0) {
104+
return false;
105+
}
106+
int k = sqrt(n);
107+
return k * k == n && (n & (n - 1)) == 0;
108+
}
109+
};
110+
```
111+
112+
## 方法三:判断是否 是2的幂且模3得1
113+
114+
如果一个数是2的幂,那么它是4的幂等价于它模3得1。
115+
116+
+ 时间复杂度$O(1)$
117+
+ 空间复杂度$O(1)$
118+
119+
### AC代码
120+
121+
#### C++
122+
123+
```cpp
124+
class Solution {
125+
public:
126+
bool isPowerOfFour(int n) {
127+
return n > 0 && (n & (n - 1)) == 0 && n % 3 == 1;
128+
}
129+
};
130+
```
131+
132+
#### Python
133+
134+
```python
135+
class Solution:
136+
def isPowerOfFour(self, n: int) -> bool:
137+
return n > 0 and n & (n - 1) == 0 and n % 3 == 1
138+
```
139+
140+
#### Java
141+
142+
```java
143+
class Solution {
144+
public boolean isPowerOfFour(int n) {
145+
return n > 0 && (n & (n - 1)) == 0 && n % 3 == 1;
146+
}
147+
}
148+
```
149+
150+
#### Go
151+
152+
```go
153+
package main
154+
155+
func isPowerOfFour(n int) bool {
156+
return n > 0 && n & (n - 1) == 0 && n % 3 == 1
157+
}
158+
```
159+
160+
#### Rust
161+
162+
```rust
163+
impl Solution {
164+
pub fn is_power_of_four(n: i32) -> bool {
165+
n > 0 && n & (n - 1) == 0 && n % 3 == 1
166+
}
167+
}
168+
```
169+
170+
## End
171+
172+
> 同步发文于[CSDN](https://letmefly.blog.csdn.net/article/details/127104821)和我的[个人博客](https://blog.letmefly.xyz/),原创不易,转载经作者同意后请附上[原文链接](https://blog.letmefly.xyz/2022/09/29/LeetCode%200342.4%E7%9A%84%E5%B9%82/)哦~
173+
>
174+
> 千篇源码题解[已开源](https://github.com/LetMeFly666/LeetCode)

Solutions/Other-English-LearningNotes-SomeWords.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1073,7 +1073,7 @@ categories: [自用]
10731073
|detract|v. 破坏,损害,转移,使分心<details><summary>例句</summary>Too much make-up will <font color="#28bea0">detract</font> from her beauty.<br/>画太浓的妆有损她的美丽。</details>|
10741074
|esteem|n. 尊重,敬重,好评<br/>v. 尊重,敬重,认为,把...看作|
10751075
|||
1076-
|piston|n. 活塞|
1076+
|<font color="#28bea0" title="二次复习">piston</font>|n. 活塞|
10771077
|||
10781078
|postponement|n. 延期,推迟|
10791079
|||
@@ -1360,6 +1360,9 @@ categories: [自用]
13601360
|commonsense|n. 常识,直觉判断力<br/>adj. 常识的,具有常识的|
13611361
|||
13621362
|solemn|adj. 严肃的,庄严的,正式的,郑重的|
1363+
|||
1364+
|conscientiously|adv. 完全地,彻底地,负责地|
1365+
|headmaster|n. (中小学)校长|
13631366

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

0 commit comments

Comments
 (0)