Skip to content

Latest commit

 

History

History
28 lines (22 loc) · 588 Bytes

03-reverse-bits.md

File metadata and controls

28 lines (22 loc) · 588 Bytes

Links

Leetcode

Expected Output

Reversed Bits integer

Brute Force

  1. Convert to a string
  2. Reverse the string
  3. Convert the reversed string to integer

Optimized

image

  1. Get the i (0....31) bit of input
  2. Replace the bit in 31 - i bit of output

Approach

class Solution:
    def reverseBits(self, n: int) -> int:
        res = 0

        for i in range(32):
            bit = (n >> i) & 1
            res = res | (bit << (31 - i))
        
        return res