← Home

405. Convert a Number to Hexadecimal

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

This problem looks busy at first, but the accepted solution is built around one steady invariant. For 405. Convert a Number to Hexadecimal, the solution in this repository is mainly a straightforward implementation solution.

Guide

What?

Before optimizing anything, pin down what information is still useful after each move. 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?

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 toHex.

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. Start from the smallest reliable state.
  2. Expand one legal move at a time.
  3. Cache, count, or merge information as soon as it becomes settled.
  4. Let the final stored value answer the original question.

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 Convert a Number to Hexadecimal.
02//Memory Usage: 8.1 MB, less than 100.00% of C++ online submissions for Convert a Number to Hexadecimal.
03
04class Solution {
05public:
06    string toHex(int num) {
07        if(num == 0) return "0";
08        
09        string ans;
10        vector digits = {'0', '1', '2', '3', '4', '5', '6', '7',
11                        '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
12        
13        unsigned int unum;
14        
15        if(num < 0) unum = UINT_MAX + 1 + (unsigned int)num;
16        else unum = num;
17        
18        while(unum > 0){
19            ans = digits[unum%16] + ans;
20            unum/=16;
21        }
22        
23        return ans;
24    }
25};
26
27//https://leetcode.com/problems/convert-a-number-to-hexadecimal/discuss/89253/Simple-Java-solution-with-comment
28//Runtime: 4 ms, faster than 100.00% of C++ online submissions for Convert a Number to Hexadecimal.
29//Memory Usage: 8 MB, less than 100.00% of C++ online submissions for Convert a Number to Hexadecimal.
30
31/**
32class Solution {
33public:
34    string toHex(int num) {
35        vector<char> d = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
36        string ans = "";
37        unsigned int unum = num;
38        
39        while(unum != 0){
40            ans = d[unum & 15] + ans;
41            unum >>= 4;
42        }
43        return ans.empty() ? "0" : ans;
44    }
45};
46**/

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.