← Home

476. Number Complement

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

A good way into this one is to ask: what do we need to remember so we never redo work blindly? For 476. Number Complement, the solution in this repository is mainly a bit manipulation solution.

Guide

What?

The code is easier to read if we treat it as a controlled search through possible states. 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?

Reach for this shape when a direct simulation would work logically but waste time revisiting the same information. The accepted code reduces that pressure by storing exactly the information that remains useful later.

The important function names to track are findComplement.

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. Read the setup variables first.
  2. Follow the main loop or recursive helper next.
  3. Watch where invalid states get skipped.
  4. Check which value survives to the return statement.

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: 0 ms, faster than 100.00% of C++ online submissions for Number Complement.
02//Memory Usage: 6.2 MB, less than 100.00% of C++ online submissions for Number Complement.
03class Solution {
04public:
05    int findComplement(int num) {
06        if(num == 1) return 0;
07        int p = floor(log2(num)) + 1;
08        
09        return (long)(1 << p) -1 - num;
10    }
11};
12
13//https://leetcode.com/problems/number-complement/discuss/96017/3-line-C%2B%2B
14//Runtime: 0 ms, faster than 100.00% of C++ online submissions for Number Complement.
15//Memory Usage: 5.9 MB, less than 100.00% of C++ online submissions for Number Complement./
16class Solution {
17public:
18    int findComplement(int num) {
19        /*
20        suppose num is 5, which is 0....0101,
21        we know that the first '1' of binary representation of 5 appears at 2nd digit
22        we want to generate a mask s.t. 
23        0...0111, (31-3rd digit is 0, and 2nd,1st,0th digit is 1)
24        and then just do xor to get the complement
25        */
26        //1...1(32 bits)
27        unsigned int mask = ~0;
28        //we want mask and num's intersection be 0
29        while(mask & num){
30            mask <<= 1;
31        }
32        //now we use ~mask
33        return ~mask ^ num;
34    }
35};

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.