Skip to content

Commit 22e883e

Browse files
committedMay 31, 2021
feat: add solutions to lc problems: No.0231,0342
1 parent 724aff7 commit 22e883e

File tree

14 files changed

+193
-25
lines changed

14 files changed

+193
-25
lines changed
 

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

+39-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# [231. 2的幂](https://leetcode-cn.com/problems/power-of-two)
1+
# [231. 2 的幂](https://leetcode-cn.com/problems/power-of-two)
22

33
[English Version](/solution/0200-0299/0231.Power%20of%20Two/README_EN.md)
44

@@ -25,7 +25,6 @@
2525
<pre><strong>输入:</strong> 218
2626
<strong>输出:</strong> false</pre>
2727

28-
2928
## 解法
3029

3130
<!-- 这里可写通用的实现逻辑 -->
@@ -37,15 +36,52 @@
3736
<!-- 这里可写当前语言的特殊实现逻辑 -->
3837

3938
```python
40-
39+
class Solution:
40+
def isPowerOfTwo(self, n: int) -> bool:
41+
return n > 0 and (n & (n - 1)) == 0
4142
```
4243

4344
### **Java**
4445

4546
<!-- 这里可写当前语言的特殊实现逻辑 -->
4647

4748
```java
49+
class Solution {
50+
public boolean isPowerOfTwo(int n) {
51+
return n > 0 && (n & (n - 1)) == 0;
52+
}
53+
}
54+
```
55+
56+
### **C++**
57+
58+
```cpp
59+
class Solution {
60+
public:
61+
bool isPowerOfTwo(int n) {
62+
return n > 0 && (n & (n - 1)) == 0;
63+
}
64+
};
65+
```
66+
67+
### **JavaScript**
68+
69+
```js
70+
/**
71+
* @param {number} n
72+
* @return {boolean}
73+
*/
74+
var isPowerOfTwo = function(n) {
75+
return n > 0 && (n & (n - 1)) == 0;
76+
};
77+
```
78+
79+
### **Go**
4880

81+
```go
82+
func isPowerOfTwo(n int) bool {
83+
return n > 0 && (n & (n - 1)) == 0
84+
}
4985
```
5086

5187
### **...**

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

+38-1
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,50 @@
6363
### **Python3**
6464

6565
```python
66-
66+
class Solution:
67+
def isPowerOfTwo(self, n: int) -> bool:
68+
return n > 0 and (n & (n - 1)) == 0
6769
```
6870

6971
### **Java**
7072

7173
```java
74+
class Solution {
75+
public boolean isPowerOfTwo(int n) {
76+
return n > 0 && (n & (n - 1)) == 0;
77+
}
78+
}
79+
```
80+
81+
### **C++**
82+
83+
```cpp
84+
class Solution {
85+
public:
86+
bool isPowerOfTwo(int n) {
87+
return n > 0 && (n & (n - 1)) == 0;
88+
}
89+
};
90+
```
91+
92+
### **JavaScript**
93+
94+
```js
95+
/**
96+
* @param {number} n
97+
* @return {boolean}
98+
*/
99+
var isPowerOfTwo = function(n) {
100+
return n > 0 && (n & (n - 1)) == 0;
101+
};
102+
```
103+
104+
### **Go**
72105

106+
```go
107+
func isPowerOfTwo(n int) bool {
108+
return n > 0 && (n & (n - 1)) == 0
109+
}
73110
```
74111

75112
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class Solution {
2+
public:
3+
bool isPowerOfTwo(int n) {
4+
return n > 0 && (n & (n - 1)) == 0;
5+
}
6+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
func isPowerOfTwo(n int) bool {
2+
return n > 0 && (n & (n - 1)) == 0
3+
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
class Solution {
22
public boolean isPowerOfTwo(int n) {
3-
return n > 0 && (n & -n) == n;
3+
return n > 0 && (n & (n - 1)) == 0;
44
}
55
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/**
2+
* @param {number} n
3+
* @return {boolean}
4+
*/
5+
var isPowerOfTwo = function(n) {
6+
return n > 0 && (n & (n - 1)) == 0;
7+
};
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,3 @@
11
class Solution:
2-
def isPowerOfTwo(self, n):
3-
"""
4-
:type n: int
5-
:rtype: bool
6-
"""
7-
if n<=0:
8-
return False
9-
m=2**32
10-
if m%n:
11-
return False
12-
else:
13-
return True
2+
def isPowerOfTwo(self, n: int) -> bool:
3+
return n > 0 and (n & (n - 1)) == 0

‎solution/0300-0399/0342.Power of Four/README.md

+38-1
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,52 @@
6161
<!-- 这里可写当前语言的特殊实现逻辑 -->
6262

6363
```python
64-
64+
class Solution:
65+
def isPowerOfFour(self, n: int) -> bool:
66+
return n > 0 and (n & (n - 1)) == 0 and (n & 0xaaaaaaaa) == 0
6567
```
6668

6769
### **Java**
6870

6971
<!-- 这里可写当前语言的特殊实现逻辑 -->
7072

7173
```java
74+
class Solution {
75+
public boolean isPowerOfFour(int n) {
76+
return n > 0 && (n & (n - 1)) == 0 && (n & 0xaaaaaaaa) == 0;
77+
}
78+
}
79+
```
80+
81+
### **C++**
82+
83+
```cpp
84+
class Solution {
85+
public:
86+
bool isPowerOfFour(int n) {
87+
return n > 0 && (n & (n - 1)) == 0 && (n & 0xaaaaaaaa) == 0;
88+
}
89+
};
90+
```
91+
92+
### **JavaScript**
93+
94+
```js
95+
/**
96+
* @param {number} n
97+
* @return {boolean}
98+
*/
99+
var isPowerOfFour = function(n) {
100+
return n > 0 && (n & (n - 1)) == 0 && (n & 0xaaaaaaaa) == 0;
101+
};
102+
```
103+
104+
### **Go**
72105

106+
```go
107+
func isPowerOfFour(n int) bool {
108+
return n > 0 && (n & (n - 1)) == 0 && (n & 0xaaaaaaaa) == 0
109+
}
73110
```
74111

75112
### **...**

‎solution/0300-0399/0342.Power of Four/README_EN.md

+38-1
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,50 @@
3636
### **Python3**
3737

3838
```python
39-
39+
class Solution:
40+
def isPowerOfFour(self, n: int) -> bool:
41+
return n > 0 and (n & (n - 1)) == 0 and (n & 0xaaaaaaaa) == 0
4042
```
4143

4244
### **Java**
4345

4446
```java
47+
class Solution {
48+
public boolean isPowerOfFour(int n) {
49+
return n > 0 && (n & (n - 1)) == 0 && (n & 0xaaaaaaaa) == 0;
50+
}
51+
}
52+
```
53+
54+
### **C++**
55+
56+
```cpp
57+
class Solution {
58+
public:
59+
bool isPowerOfFour(int n) {
60+
return n > 0 && (n & (n - 1)) == 0 && (n & 0xaaaaaaaa) == 0;
61+
}
62+
};
63+
```
64+
65+
### **JavaScript**
66+
67+
```js
68+
/**
69+
* @param {number} n
70+
* @return {boolean}
71+
*/
72+
var isPowerOfFour = function(n) {
73+
return n > 0 && (n & (n - 1)) == 0 && (n & 0xaaaaaaaa) == 0;
74+
};
75+
```
76+
77+
### **Go**
4578

79+
```go
80+
func isPowerOfFour(n int) bool {
81+
return n > 0 && (n & (n - 1)) == 0 && (n & 0xaaaaaaaa) == 0
82+
}
4683
```
4784

4885
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class Solution {
2+
public:
3+
bool isPowerOfFour(int n) {
4+
return n > 0 && (n & (n - 1)) == 0 && (n & 0xaaaaaaaa) == 0;
5+
}
6+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
func isPowerOfFour(n int) bool {
2+
return n > 0 && (n & (n - 1)) == 0 && (n & 0xaaaaaaaa) == 0
3+
}
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
public class Solution {
1+
class Solution {
22
public boolean isPowerOfFour(int n) {
3-
if(n <= 0) return false;
4-
return ((n & (n - 1)) == 0) && ((n & 0x55555555) != 0);
3+
return n > 0 && (n & (n - 1)) == 0 && (n & 0xaaaaaaaa) == 0;
54
}
65
}
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1-
const isPowerOfFour = function (num) {
2-
return (Math.log(num) / Math.log(4)) % 1.0 == 0.0;
3-
};
1+
/**
2+
* @param {number} n
3+
* @return {boolean}
4+
*/
5+
var isPowerOfFour = function(n) {
6+
return n > 0 && (n & (n - 1)) == 0 && (n & 0xaaaaaaaa) == 0;
7+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class Solution:
2+
def isPowerOfFour(self, n: int) -> bool:
3+
return n > 0 and (n & (n - 1)) == 0 and (n & 0xaaaaaaaa) == 0

0 commit comments

Comments
 (0)