← Home

190. Reverse Bits

LeetCode article · C++ solution
Website made by wuisabel-gif · Original C++ code by keineahnung2345
bit manipulationC++Markdown
190

I like to read this solution as a small machine: keep the useful information, throw away the noise. For 190. Reverse Bits, the solution in this repository is mainly a bit manipulation solution.

Guide

What?

The first job is to translate the English prompt into state, transition, and stopping conditions. Instead of trying to be clever immediately, read the code as a sequence of questions:

  • What state are we keeping?
  • How do we move from one state to the next?
  • When do we know the answer is already determined?

For this file, the main tools are: bit manipulation.

Guide

When?

This pattern shows up when the brute force version has too many repeated checks, too many possible branches, or too much bookkeeping to do by hand. The accepted code reduces that pressure by storing exactly the information that remains useful later.

The solution is organized around the main LeetCode entry point and a few local helpers.

Guide

Why?

The code is doing bookkeeping so your brain does not have to keep the entire search space open at once.

  • The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.

Guide

How?

Walk through the solution in this order:

  1. Initialize the memory or helper structure.
  2. Process candidates in the order the invariant expects.
  3. Update the answer only when the current state is valid.
  4. Return the value that represents the fully processed input.

The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.

Guide

Complexity

  • Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
  • Space: O(n) in the usual case for auxiliary containers or recursion

Guide

C++ Solution

Your submission

The accepted solution

solution.cpp
01//Runtime: 4 ms, faster than 100.00% of C++ online submissions for Reverse Bits.
02//Memory Usage: 7.9 MB, less than 85.71% of C++ online submissions for Reverse Bits.
03
04class Solution {
05public:
06    uint32_t reverseBits(uint32_t n) {
07        // cout << numeric_limits<uint32_t>::max() << endl;
08        // // return numeric_limits<uint32_t>::max() - n;
09        // return numeric_limits<uint32_t>::max();
10        uint32_t x = 0;
11        int d = 0;
12        while(n > 0){
13            // cout << d << " " << n << endl;
14            x += (n%2) << (31 - d);
15            n >>= 1;
16            d++;
17        }
18        return x;
19    }
20};

Cost

Complexity

Time
O(n) to O(n log n), depending on the dominant loop or data structure operation
Dominated by the main traversal, recursion, or data-structure operations in the code.
Space
O(n) in the usual case for auxiliary containers or recursion
Auxiliary state plus the answer structure where the problem requires one.