← Home

1012. Complement of Base 10 Integer

LeetCode article · C++ solution
Website made by wuisabel-gif · Original C++ code by keineahnung2345
straightforward implementationC++Markdown
101

I like to read this solution as a small machine: keep the useful information, throw away the noise. For 1012. Complement of Base 10 Integer, the solution in this repository is mainly a straightforward implementation 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: straightforward implementation.

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 important function names to track are bitwiseComplement.

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/**
02Every non-negative integer N has a binary representation.  For example, 5 can be represented as "101" in binary, 11 as "1011" in binary, and so on.  Note that except for N = 0, there are no leading zeroes in any binary representation.
03
04The complement of a binary representation is the number in binary you get when changing every 1 to a 0 and 0 to a 1.  For example, the complement of "101" in binary is "010" in binary.
05
06For a given number N in base-10, return the complement of it's binary representation as a base-10 integer.
07
08 
09
10Example 1:
11
12Input: 5
13Output: 2
14Explanation: 5 is "101" in binary, with complement "010" in binary, which is 2 in base-10.
15Example 2:
16
17Input: 7
18Output: 0
19Explanation: 7 is "111" in binary, with complement "000" in binary, which is 0 in base-10.
20Example 3:
21
22Input: 10
23Output: 5
24Explanation: 10 is "1010" in binary, with complement "0101" in binary, which is 5 in base-10.
25 
26
27Note:
28
290 <= N < 10^9
30**/
31
32//slow
33//Runtime: 8 ms, faster than 10.65% of C++ online submissions for Complement of Base 10 Integer.
34//Memory Usage: 8.5 MB, less than 100.00% of C++ online submissions for Complement of Base 10 Integer.
35//fast
36//Runtime: 4 ms, faster than 100.00% of C++ online submissions for Complement of Base 10 Integer.
37//Memory Usage: 8.3 MB, less than 100.00% of C++ online submissions for Complement of Base 10 Integer.
38class Solution {
39public:
40    int bitwiseComplement(int N) {
41        if(N==0) return 1;
42        
43        vector<int> v;
44        while(N > 0){
45            v.push_back(N%2);
46            N/=2;
47        }
48        
49        int ans = 0;
50        
51        for(int i = v.size()-1; i >=0; i--){
52            if(v[i]==0){
53                //ans += pow(2, i); //slow
54                ans += 1<<i; //fast
55            }
56        }
57        
58        return ans;
59    }
60};

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.