Advertisement

Reverse Bits - LeetCode 190 Solution

Reverse Bits - Complete Solution Guide

Reverse Bits is LeetCode problem 190, a Easy level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

Reverse bits of a given 32 bits unsigned integer. Note: Note that in some languages, such as Java, there is no unsigned integer type. In this case, both input and output will be given as a signed integer type. They should not affect your implementation, as the integer's internal binary representation is the same, whether it is signed or unsigned. In Java, the compiler represents the signed integers using 2's complement notation . Example 1: Input: n = 43261596 Output: 964176192 Explanation: Inte

Detailed Explanation

The problem asks you to reverse the bits of a 32-bit unsigned integer. The input is a single integer, `n`. The output is the integer formed by reversing the order of its bits. For example, if the input is represented in binary as `00000010100101000001111010011100`, the output should be `00111001011110000010100101000000`. The problem emphasizes that even though some languages (like Java) don't have unsigned integers, the underlying binary representation remains the same and should be treated as such.

Solution Approach

The provided solutions employ a loop that iterates 32 times (once for each bit). In each iteration, the least significant bit (LSB) of the input `n` is extracted using a bitwise AND operation (`n & 1`). This LSB is then shifted to its corresponding reversed position in the `result` variable using a left bit shift (`<<`). The input `n` is then right-shifted (`>>` in Python, C++, `>>>` in Java) to prepare for the next bit. This process continues until all 32 bits are processed.

Step-by-Step Algorithm

  1. Step 1: Initialize a `result` variable to 0. This variable will store the reversed bits.
  2. Step 2: Iterate 32 times using a `for` loop (representing the 32 bits).
  3. Step 3: Extract the LSB of `n` using `n & 1`.
  4. Step 4: Shift the extracted LSB to its reversed position in `result` using `bit << (31 - i)` (Python) or `result <<= 1; result |= (n & 1);` (C++, Java, C).
  5. Step 5: Right-shift `n` by 1 bit (`n >>= 1` or `n >>>= 1`) to process the next bit.
  6. Step 6: After the loop completes, `result` contains the reversed bits of `n`.

Key Insights

  • Insight 1: Understanding bitwise operators is crucial. We need to use bit shifting (`<<`, `>>`, `>>>`) and bitwise AND (`&`) to isolate and manipulate individual bits.
  • Insight 2: Iterating through the bits of the integer is necessary to reverse them one by one. A loop is the natural choice for this task.
  • Insight 3: The use of a bitmask (`& 1`) efficiently isolates the least significant bit (LSB) in each iteration.

Complexity Analysis

Time Complexity: O(1)

Space Complexity: O(1)

Topics

This problem involves: Divide and Conquer, Bit Manipulation.

Companies

Asked at: Airbnb, Nvidia, Qualcomm.