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 efb1ee4

Browse files
committed
feat: update solutions to lc problem: No.0231
No.0231.Power of Two
1 parent 4832e79 commit efb1ee4

File tree

4 files changed

+121
-6
lines changed

4 files changed

+121
-6
lines changed

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

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@
2929

3030
<!-- 这里可写通用的实现逻辑 -->
3131

32+
1. `n & (n - 1)` 可将最后一个二进制形式的 n 的最后一位 1 移除,若移除后为 0,说明 n 是 2 的幂。
33+
2. lowbit:`n & (-n)` 可以得到 n 的最后一位 1 表示的十进制数,若与 n 相等,说明 n 是 2 的幂。
34+
3235
<!-- tabs:start -->
3336

3437
### **Python3**
@@ -41,6 +44,14 @@ class Solution:
4144
return n > 0 and (n & (n - 1)) == 0
4245
```
4346

47+
lowbit:
48+
49+
```python
50+
class Solution:
51+
def isPowerOfTwo(self, n: int) -> bool:
52+
return n > 0 and n == n & (-n)
53+
```
54+
4455
### **Java**
4556

4657
<!-- 这里可写当前语言的特殊实现逻辑 -->
@@ -53,6 +64,16 @@ class Solution {
5364
}
5465
```
5566

67+
lowbit:
68+
69+
```java
70+
class Solution {
71+
public boolean isPowerOfTwo(int n) {
72+
return n > 0 && n == (n & (-n));
73+
}
74+
}
75+
```
76+
5677
### **C++**
5778

5879
```cpp
@@ -64,6 +85,17 @@ public:
6485
};
6586
```
6687
88+
lowbit:
89+
90+
```cpp
91+
class Solution {
92+
public:
93+
bool isPowerOfTwo(int n) {
94+
return n > 0 && n == (n & (-n));
95+
}
96+
};
97+
```
98+
6799
### **JavaScript**
68100

69101
```js
@@ -76,19 +108,47 @@ var isPowerOfTwo = function(n) {
76108
};
77109
```
78110

111+
lowbit:
112+
113+
```js
114+
/**
115+
* @param {number} n
116+
* @return {boolean}
117+
*/
118+
var isPowerOfTwo = function(n) {
119+
return n > 0 && n == (n & -n);
120+
};
121+
```
122+
79123
### **Go**
80124

81125
```go
82126
func isPowerOfTwo(n int) bool {
83-
return n > 0 && (n & (n - 1)) == 0
127+
return n > 0 && (n&(n-1)) == 0
128+
}
129+
```
130+
131+
lowbit:
132+
133+
```go
134+
func isPowerOfTwo(n int) bool {
135+
return n > 0 && n == (n&(-n))
84136
}
85137
```
86138

87139
### **TypeScript**
88140

89141
```ts
90142
function isPowerOfTwo(n: number): boolean {
91-
return n > 0 && (n & (n - 1)) == 0;
143+
return n > 0 && (n & (n - 1)) == 0;
144+
};
145+
```
146+
147+
lowbit:
148+
149+
```ts
150+
function isPowerOfTwo(n: number): boolean {
151+
return n > 0 && (n & (n - 1)) == 0;
92152
};
93153
```
94154

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

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,14 @@ class Solution:
6868
return n > 0 and (n & (n - 1)) == 0
6969
```
7070

71+
lowbit:
72+
73+
```python
74+
class Solution:
75+
def isPowerOfTwo(self, n: int) -> bool:
76+
return n > 0 and n == n & (-n)
77+
```
78+
7179
### **Java**
7280

7381
```java
@@ -78,6 +86,16 @@ class Solution {
7886
}
7987
```
8088

89+
lowbit:
90+
91+
```java
92+
class Solution {
93+
public boolean isPowerOfTwo(int n) {
94+
return n > 0 && n == (n & (-n));
95+
}
96+
}
97+
```
98+
8199
### **C++**
82100

83101
```cpp
@@ -89,6 +107,15 @@ public:
89107
};
90108
```
91109
110+
```cpp
111+
class Solution {
112+
public:
113+
bool isPowerOfTwo(int n) {
114+
return n > 0 && n == (n & (-n));
115+
}
116+
};
117+
```
118+
92119
### **JavaScript**
93120

94121
```js
@@ -101,19 +128,47 @@ var isPowerOfTwo = function(n) {
101128
};
102129
```
103130

131+
lowbit:
132+
133+
```js
134+
/**
135+
* @param {number} n
136+
* @return {boolean}
137+
*/
138+
var isPowerOfTwo = function(n) {
139+
return n > 0 && n == (n & (-n));
140+
};
141+
```
142+
104143
### **Go**
105144

106145
```go
107146
func isPowerOfTwo(n int) bool {
108-
return n > 0 && (n & (n - 1)) == 0
147+
return n > 0 && (n&(n-1)) == 0
148+
}
149+
```
150+
151+
lowbit:
152+
153+
```go
154+
func isPowerOfTwo(n int) bool {
155+
return n > 0 && n == (n&(-n))
109156
}
110157
```
111158

112159
### **TypeScript**
113160

114161
```ts
115162
function isPowerOfTwo(n: number): boolean {
116-
return n > 0 && (n & (n - 1)) == 0;
163+
return n > 0 && (n & (n - 1)) == 0;
164+
};
165+
```
166+
167+
lowbit:
168+
169+
```ts
170+
function isPowerOfTwo(n: number): boolean {
171+
return n > 0 && n == (n & (-n));
117172
};
118173
```
119174

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
func isPowerOfTwo(n int) bool {
2-
return n > 0 && (n& (n-1)) == 0
2+
return n > 0 && (n&(n-1)) == 0
33
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
function isPowerOfTwo(n: number): boolean {
22
return n > 0 && (n & (n - 1)) == 0;
3-
};
3+
};

0 commit comments

Comments
(0)

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