Skip to content

Commit e5b79f6

Browse files
authored
Create 0231-power-of-two.py
1 parent bdddebd commit e5b79f6

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

python/0231-power-of-two.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# iterative
2+
class Solution:
3+
def isPowerOfTwo(self, n: int) -> bool:
4+
x = 1
5+
while x < n:
6+
x *= 2
7+
return x == n
8+
9+
# Bit manipulation
10+
class Solution:
11+
def isPowerOfTwo(self, n: int) -> bool:
12+
return n > 0 and (n & (n - 1)) == 0
13+
14+
# Bit manipulation
15+
class Solution:
16+
def isPowerOfTwo(self, n: int) -> bool:
17+
return n > 0 and ((1 << 30) % n) == 0

0 commit comments

Comments
 (0)