Skip to content

Commit 6c3f9f5

Browse files
authored
feat: add solutions to lc problem: No.0342 (#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)