Skip to content

feat: add solutions to lc problem: No.0231 #4631

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 8, 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
34 changes: 28 additions & 6 deletions solution/0200-0299/0231.Power of Two/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ tags:

### 方法一:位运算

$\texttt{n\&(n-1)}$ 可将最后一个二进制形式的 $n$ 的最后一位 $1$ 移除,若移除后为 $0$,说明 $n$ 是 $2$ 的幂。
根据位运算的性质,执行 $\texttt{n\&(n-1)}$ 可以消去二进制形式的 $n$ 的最后一位 $1$。因此,如果 $n \gt 0$,并且满足 $\texttt{n\&(n-1)}$ 结果为 $0$,则说明 $n$ 是 $2$ 的幂。

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

<!-- tabs:start -->

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

#### Rust

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

#### JavaScript

```js
Expand All @@ -134,11 +146,11 @@ var isPowerOfTwo = function (n) {

<!-- solution:start -->

### 方法二:lowbit
### 方法二:Lowbit

$\texttt{n\&(-n)}$ 可以得到 $n$ 的最后一位 $1$ 表示的十进制数,若与 $n$ 相等,说明 $n$ 是 $2$ 的幂。
根据 $\text{lowbit}$ 的定义,我们知道 $\text{lowbit}(x) = x \& (-x)$,可以得到 $n$ 的最后一位 $1$ 表示的十进制数。因此,如果 $n > 0$,并且满足 $\text{lowbit}(n)$ 等于 $n$,则说明 $n$ 是 $2$ 的幂。

注意:要满足 $n$ 是 $2$ 的幂次方,需要保证 $n$ 大于 $0$。
时间复杂度 $O(1)$,空间复杂度 $O(1)$。

<!-- tabs:start -->

Expand Down Expand Up @@ -183,7 +195,17 @@ func isPowerOfTwo(n int) bool {

```ts
function isPowerOfTwo(n: number): boolean {
return n > 0 && (n & (n - 1)) === 0;
return n > 0 && n === (n & -n);
}
```

#### Rust

```rust
impl Solution {
pub fn is_power_of_two(n: i32) -> bool {
n > 0 && n == (n & (-n))
}
}
```

Expand All @@ -195,7 +217,7 @@ function isPowerOfTwo(n: number): boolean {
* @return {boolean}
*/
var isPowerOfTwo = function (n) {
return n > 0 && n == (n & -n);
return n > 0 && n === (n & -n);
};
```

Expand Down
36 changes: 32 additions & 4 deletions solution/0200-0299/0231.Power of Two/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,11 @@ tags:

<!-- solution:start -->

### Solution 1
### Solution 1: Bit Manipulation

According to the properties of bit manipulation, executing $\texttt{n\&(n-1)}$ can eliminate the last bit $1$ in the binary form of $n$. Therefore, if $n > 0$ and $\texttt{n\&(n-1)}$ results in $0$, then $n$ is a power of $2$.

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

<!-- tabs:start -->

Expand Down Expand Up @@ -111,6 +115,16 @@ function isPowerOfTwo(n: number): boolean {
}
```

#### Rust

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

#### JavaScript

```js
Expand All @@ -129,7 +143,11 @@ var isPowerOfTwo = function (n) {

<!-- solution:start -->

### Solution 2
### Solution 2: Lowbit

According to the definition of $\text{lowbit}$, we know that $\text{lowbit}(x) = x \& (-x)$, which can get the decimal number represented by the last bit $1$ of $n$. Therefore, if $n > 0$ and $\text{lowbit}(n)$ equals $n$, then $n$ is a power of $2$.

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

<!-- tabs:start -->

Expand Down Expand Up @@ -174,7 +192,17 @@ func isPowerOfTwo(n int) bool {

```ts
function isPowerOfTwo(n: number): boolean {
return n > 0 && (n & (n - 1)) === 0;
return n > 0 && n === (n & -n);
}
```

#### Rust

```rust
impl Solution {
pub fn is_power_of_two(n: i32) -> bool {
n > 0 && n == (n & (-n))
}
}
```

Expand All @@ -186,7 +214,7 @@ function isPowerOfTwo(n: number): boolean {
* @return {boolean}
*/
var isPowerOfTwo = function (n) {
return n > 0 && n == (n & -n);
return n > 0 && n === (n & -n);
};
```

Expand Down
5 changes: 5 additions & 0 deletions solution/0200-0299/0231.Power of Two/Solution.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
impl Solution {
pub fn is_power_of_two(n: i32) -> bool {
n > 0 && (n & (n - 1)) == 0
}
}
2 changes: 1 addition & 1 deletion solution/0200-0299/0231.Power of Two/Solution2.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
* @return {boolean}
*/
var isPowerOfTwo = function (n) {
return n > 0 && n == (n & -n);
return n > 0 && n === (n & -n);
};
5 changes: 5 additions & 0 deletions solution/0200-0299/0231.Power of Two/Solution2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
impl Solution {
pub fn is_power_of_two(n: i32) -> bool {
n > 0 && n == (n & (-n))
}
}
2 changes: 1 addition & 1 deletion solution/0200-0299/0231.Power of Two/Solution2.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
function isPowerOfTwo(n: number): boolean {
return n > 0 && (n & (n - 1)) === 0;
return n > 0 && n === (n & -n);
}