335. Convert a Number to Hexadecimal

Given an integer num, convert it to its hexadecimal representation as a string.

The returned hexadecimal string should:

  • Use lowercase letters 'a' to 'f' for digits 10 to 15.
  • Not contain any leading zeros.
  • For negative numbers, use two’s complement (as done in 32-bit signed integer representation).

Example 1:

Input: num = 26

Output: "1a"

Example 2:

Input: num = -1

Output: "ffffffff"

Now Your Turn!

Pick the correct output for the given input

Input: num = 0

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • -231 <= num <= 231 - 1
0
class Solution {
public:
string toHex(int num) {
// Your code goes here
}
};
Test Case

Input:

Num