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 28073f8

Browse files
feat: add solutions to lc problem: No.0231 (#4631)
No.0231.Power of Two
1 parent 5e77929 commit 28073f8

File tree

6 files changed

+72
-12
lines changed

6 files changed

+72
-12
lines changed

‎solution/0200-0299/0231.Power of Two/README.md

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,9 @@ tags:
6767

6868
### 方法一:位运算
6969

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

7274
<!-- tabs:start -->
7375

@@ -116,6 +118,16 @@ function isPowerOfTwo(n: number): boolean {
116118
}
117119
```
118120

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

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

135147
<!-- solution:start -->
136148

137-
### 方法二:lowbit
149+
### 方法二:Lowbit
138150

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

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

143155
<!-- tabs:start -->
144156

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

184196
```ts
185197
function isPowerOfTwo(n: number): boolean {
186-
return n > 0 && (n & (n - 1)) === 0;
198+
return n > 0 && n === (n & -n);
199+
}
200+
```
201+
202+
#### Rust
203+
204+
```rust
205+
impl Solution {
206+
pub fn is_power_of_two(n: i32) -> bool {
207+
n > 0 && n == (n & (-n))
208+
}
187209
}
188210
```
189211

@@ -195,7 +217,7 @@ function isPowerOfTwo(n: number): boolean {
195217
* @return {boolean}
196218
*/
197219
var isPowerOfTwo = function (n) {
198-
return n > 0 && n == (n & -n);
220+
return n > 0 && n === (n & -n);
199221
};
200222
```
201223

‎solution/0200-0299/0231.Power of Two/README_EN.md

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,11 @@ tags:
6262

6363
<!-- solution:start -->
6464

65-
### Solution 1
65+
### Solution 1: Bit Manipulation
66+
67+
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ドル$.
68+
69+
The time complexity is $O(1),ドル and the space complexity is $O(1)$.
6670

6771
<!-- tabs:start -->
6872

@@ -111,6 +115,16 @@ function isPowerOfTwo(n: number): boolean {
111115
}
112116
```
113117

118+
#### Rust
119+
120+
```rust
121+
impl Solution {
122+
pub fn is_power_of_two(n: i32) -> bool {
123+
n > 0 && (n & (n - 1)) == 0
124+
}
125+
}
126+
```
127+
114128
#### JavaScript
115129

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

130144
<!-- solution:start -->
131145

132-
### Solution 2
146+
### Solution 2: Lowbit
147+
148+
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ドル$.
149+
150+
The time complexity is $O(1),ドル and the space complexity is $O(1)$.
133151

134152
<!-- tabs:start -->
135153

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

175193
```ts
176194
function isPowerOfTwo(n: number): boolean {
177-
return n > 0 && (n & (n - 1)) === 0;
195+
return n > 0 && n === (n & -n);
196+
}
197+
```
198+
199+
#### Rust
200+
201+
```rust
202+
impl Solution {
203+
pub fn is_power_of_two(n: i32) -> bool {
204+
n > 0 && n == (n & (-n))
205+
}
178206
}
179207
```
180208

@@ -186,7 +214,7 @@ function isPowerOfTwo(n: number): boolean {
186214
* @return {boolean}
187215
*/
188216
var isPowerOfTwo = function (n) {
189-
return n > 0 && n == (n & -n);
217+
return n > 0 && n === (n & -n);
190218
};
191219
```
192220

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_two(n: i32) -> bool {
3+
n > 0 && (n & (n - 1)) == 0
4+
}
5+
}

‎solution/0200-0299/0231.Power of Two/Solution2.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33
* @return {boolean}
44
*/
55
var isPowerOfTwo = function (n) {
6-
return n > 0 && n == (n & -n);
6+
return n > 0 && n === (n & -n);
77
};
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_two(n: i32) -> bool {
3+
n > 0 && n == (n & (-n))
4+
}
5+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
function isPowerOfTwo(n: number): boolean {
2-
return n > 0 && (n& (n -1))===0;
2+
return n > 0 && n=== (n &-n);
33
}

0 commit comments

Comments
(0)

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