Skip to content

Commit 23d3b01

Browse files
authored
Create 231_Power_of_Two.md
1 parent 1c90ee1 commit 23d3b01

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

231_Power_of_Two.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
## 231. Power of Two
2+
3+
Given an integer n, write a function to determine if it is a power of two.
4+
5+
6+
7+
Example 1:
8+
9+
Input: n = 1
10+
Output: true
11+
Explanation: 20 = 1
12+
Example 2:
13+
14+
Input: n = 16
15+
Output: true
16+
Explanation: 24 = 16
17+
Example 3:
18+
19+
Input: n = 3
20+
Output: false
21+
Example 4:
22+
23+
Input: n = 4
24+
Output: true
25+
Example 5:
26+
27+
Input: n = 5
28+
Output: false
29+
30+
31+
Constraints:
32+
33+
-231 <= n <= 231 - 1
34+
35+
```python
36+
def isPowerOfTwo(self, n: int) -> bool:
37+
# return n > 0 and n == n & -n
38+
if not n or n < 1: return False
39+
return n == n & (-n)
40+
```
41+
42+
```
43+
Runtime: 24 ms, faster than 95.06% of Python3 online submissions for Power of Two.
44+
Memory Usage: 14.1 MB, less than 99.94% of Python3 online submissions for Power of Two.
45+
```

0 commit comments

Comments
 (0)