Skip to content

feat: add solutions to lc problem: No.0342 #4647

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions solution/0300-0399/0342.Power of Four/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ tags:

### 方法一:位运算

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

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

时间复杂度 $O(1)$,空间复杂度 $O(1)$。

Expand Down Expand Up @@ -118,6 +118,16 @@ function isPowerOfFour(n: number): boolean {
}
```

#### Rust

```rust
impl Solution {
pub fn is_power_of_four(n: i32) -> bool {
n > 0 && (n & (n - 1)) == 0 && (n & 0xaaaaaaaa_u32 as i32) == 0
}
}
```

#### JavaScript

```js
Expand All @@ -130,6 +140,16 @@ var isPowerOfFour = function (n) {
};
```

#### C#

```cs
public class Solution {
public bool IsPowerOfFour(int n) {
return n > 0 && (n & (n - 1)) == 0 && (n & 0xaaaaaaaa) == 0;
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
24 changes: 22 additions & 2 deletions solution/0300-0399/0342.Power of Four/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ tags:

### Solution 1: Bit Manipulation

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.
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.

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.
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$.

The time complexity is $O(1)$, and the space complexity is $O(1)$.

Expand Down Expand Up @@ -104,6 +104,16 @@ function isPowerOfFour(n: number): boolean {
}
```

#### Rust

```rust
impl Solution {
pub fn is_power_of_four(n: i32) -> bool {
n > 0 && (n & (n - 1)) == 0 && (n & 0xaaaaaaaa_u32 as i32) == 0
}
}
```

#### JavaScript

```js
Expand All @@ -116,6 +126,16 @@ var isPowerOfFour = function (n) {
};
```

#### C#

```cs
public class Solution {
public bool IsPowerOfFour(int n) {
return n > 0 && (n & (n - 1)) == 0 && (n & 0xaaaaaaaa) == 0;
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
5 changes: 5 additions & 0 deletions solution/0300-0399/0342.Power of Four/Solution.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
public class Solution {
public bool IsPowerOfFour(int n) {
return n > 0 && (n & (n - 1)) == 0 && (n & 0xaaaaaaaa) == 0;
}
}
5 changes: 5 additions & 0 deletions solution/0300-0399/0342.Power of Four/Solution.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
impl Solution {
pub fn is_power_of_four(n: i32) -> bool {
n > 0 && (n & (n - 1)) == 0 && (n & 0xaaaaaaaa_u32 as i32) == 0
}
}