Skip to content

Commit f0fc96e

Browse files
committed
week 8 reverse-bits
1 parent 5a40ebe commit f0fc96e

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

reverse-bits/i-mprovising.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution:
2+
"""
3+
Time, space complexity O(1)
4+
"""
5+
def reverseBits(self, n: int) -> int:
6+
stack = []
7+
while len(stack) < 32:
8+
stack.append(n % 2)
9+
n //= 2
10+
11+
reverse, x = 0, 1
12+
while stack:
13+
reverse += stack.pop() * x
14+
x *= 2
15+
16+
return reverse

0 commit comments

Comments
 (0)