Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 6c3f9f5

Browse files
feat: add solutions to lc problem: No.0342 (doocs#4647)
No.0342.Power of Four
1 parent 7564363 commit 6c3f9f5

File tree

4 files changed

+54
-4
lines changed

4 files changed

+54
-4
lines changed

‎solution/0300-0399/0342.Power of Four/README.md‎

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,9 @@ tags:
6565

6666
### 方法一:位运算
6767

68-
如果一个数是 4 的幂次方,那么这个数必须是大于 0ドル$ 的。不妨假设这个数是 4ドル^x,ドル即 2ドル^{2x},ドル那么这个数的二进制表示中有且仅有一个 1ドル,ドル且这个 1ドル$ 出现在偶数位上。
68+
如果一个数是 4ドル$ 的幂次方,那么这个数必须是大于 0ドル$ 的。不妨假设这个数是 4ドル^x,ドル即 2ドル^{2x},ドル那么这个数的二进制表示中有且仅有一个 1ドル,ドル且这个 1ドル$ 出现在偶数位上。
6969

70-
因此,我们首先判断这个数是否大于 0ドル,ドル然后判断这个数是否是 2ドル^{2x},ドル即 $n$ 与 $n-1$ 的按位与结果是否为 0ドル,ドル最后判断这个数的 1ドル$ 是否出现在偶数位上,即 $n$ 与 $\textit{0xAAAAAAAA}$ 的按位与结果是否为 0ドル$。如果这三个条件都满足,那么这个数就是 4 的幂次方。
70+
因此,我们首先判断这个数是否大于 0ドル,ドル然后判断这个数是否是 2ドル^{2x},ドル即 $n$ 与 $n-1$ 的按位与结果是否为 0ドル,ドル最后判断这个数的 1ドル$ 是否出现在偶数位上,即 $n$ 与 $\textit{0xAAAAAAAA}$ 的按位与结果是否为 0ドル$。如果这三个条件都满足,那么这个数就是 4ドル$ 的幂次方。
7171

7272
时间复杂度 $O(1),ドル空间复杂度 $O(1)$。
7373

@@ -118,6 +118,16 @@ function isPowerOfFour(n: number): boolean {
118118
}
119119
```
120120

121+
#### Rust
122+
123+
```rust
124+
impl Solution {
125+
pub fn is_power_of_four(n: i32) -> bool {
126+
n > 0 && (n & (n - 1)) == 0 && (n & 0xaaaaaaaa_u32 as i32) == 0
127+
}
128+
}
129+
```
130+
121131
#### JavaScript
122132

123133
```js
@@ -130,6 +140,16 @@ var isPowerOfFour = function (n) {
130140
};
131141
```
132142

143+
#### C#
144+
145+
```cs
146+
public class Solution {
147+
public bool IsPowerOfFour(int n) {
148+
return n > 0 && (n & (n - 1)) == 0 && (n & 0xaaaaaaaa) == 0;
149+
}
150+
}
151+
```
152+
133153
<!-- tabs:end -->
134154

135155
<!-- solution:end -->

‎solution/0300-0399/0342.Power of Four/README_EN.md‎

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ tags:
5151

5252
### Solution 1: Bit Manipulation
5353

54-
If a number is a power of 4, then it must be greater than 0ドル$. Suppose this number is 4ドル^x,ドル which is 2ドル^{2x}$. Therefore, its binary representation has only one 1ドル,ドル and this 1ドル$ appears at an even position.
54+
If a number is a power of 4ドル$, then it must be greater than 0ドル$. Suppose this number is 4ドル^x,ドル which is 2ドル^{2x}$. Therefore, its binary representation has only one 1ドル,ドル and this 1ドル$ appears at an even position.
5555

56-
First, we check if the number is greater than 0ドル$. Then, we verify if the number is 2ドル^{2x}$ by checking if the bitwise AND of $n$ and $n-1$ is 0ドル$. Finally, we check if the 1ドル$ appears at an even position by verifying if the bitwise AND of $n$ and $\textit{0xAAAAAAAA}$ is 0ドル$. If all three conditions are met, then the number is a power of 4.
56+
First, we check if the number is greater than 0ドル$. Then, we verify if the number is 2ドル^{2x}$ by checking if the bitwise AND of $n$ and $n-1$ is 0ドル$. Finally, we check if the 1ドル$ appears at an even position by verifying if the bitwise AND of $n$ and $\textit{0xAAAAAAAA}$ is 0ドル$. If all three conditions are met, then the number is a power of 4ドル$.
5757

5858
The time complexity is $O(1),ドル and the space complexity is $O(1)$.
5959

@@ -104,6 +104,16 @@ function isPowerOfFour(n: number): boolean {
104104
}
105105
```
106106

107+
#### Rust
108+
109+
```rust
110+
impl Solution {
111+
pub fn is_power_of_four(n: i32) -> bool {
112+
n > 0 && (n & (n - 1)) == 0 && (n & 0xaaaaaaaa_u32 as i32) == 0
113+
}
114+
}
115+
```
116+
107117
#### JavaScript
108118

109119
```js
@@ -116,6 +126,16 @@ var isPowerOfFour = function (n) {
116126
};
117127
```
118128

129+
#### C#
130+
131+
```cs
132+
public class Solution {
133+
public bool IsPowerOfFour(int n) {
134+
return n > 0 && (n & (n - 1)) == 0 && (n & 0xaaaaaaaa) == 0;
135+
}
136+
}
137+
```
138+
119139
<!-- tabs:end -->
120140

121141
<!-- solution:end -->
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
public class Solution {
2+
public bool IsPowerOfFour(int n) {
3+
return n > 0 && (n & (n - 1)) == 0 && (n & 0xaaaaaaaa) == 0;
4+
}
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
impl Solution {
2+
pub fn is_power_of_four(n: i32) -> bool {
3+
n > 0 && (n & (n - 1)) == 0 && (n & 0xaaaaaaaa_u32 as i32) == 0
4+
}
5+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /