Skip to content

Commit c366a00

Browse files
committed
- Reverse Bits DaleStudy#234
1 parent 4e58a77 commit c366a00

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

reverse-bits/ayosecu.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution:
2+
"""
3+
- Time Complexity: O(1), 32 times calculation
4+
- Space Complexity: O(1)
5+
"""
6+
def reverseBits(self, n: int) -> int:
7+
result = 0
8+
9+
for _ in range(32):
10+
result <<= 1
11+
result |= n & 1
12+
n >>= 1
13+
14+
return result
15+
16+
tc = [
17+
(43261596, 964176192),
18+
(4294967293, 3221225471)
19+
]
20+
21+
sol = Solution()
22+
for i, (n, e) in enumerate(tc, 1):
23+
r = sol.reverseBits(n)
24+
print(f"TC {i} is Passed!" if r == e else f"TC {i} is Failed! - Expected: {e}, Result: {r}")

0 commit comments

Comments
 (0)