Trains the technique from
LeetCode 405Convert a Number to HexadecimalThis is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.
Same function shape, different story and different numbers.
A reading num is a whole number that fits in thirty-two bits.
Write it in base sixteen, using '0' through '9' and then the lowercase letters 'a' through 'f'. A negative reading is written as its pattern over thirty-two bits taken as an unsigned number, which is the reading plus two raised to the thirty-second. Write no leading zero, except that the reading 0 is written as the single character "0".
Return that spelling.
Example 1
Two hundred and fifty-five is fifteen sixteens plus fifteen, and fifteen is written as the letter f.
Example 2
The reading is nothing, which is written as a single zero rather than as no digits at all.
Example 3
Adding two raised to the thirty-second turns this reading into two raised to the thirty-first, which in base sixteen is an eight followed by seven zeros.
The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.
def to_hex(num: int) -> str:public String toHex(int num)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.