Following is the problem statement from LeetCode.
Given an integer, write an algorithm to convert it to hexadecimal. For negative integer, two’s complement method is used.
Note:
All letters in hexadecimal (a-f) must be in lowercase.
The hexadecimal string must not contain extra leading 0s. If the number is zero, it is represented by a single zero character '0'; otherwise, the first character in the hexadecimal string will not be the zero character.
The given number is guaranteed to fit within the range of a 32-bit signed integer.
You must not use any method provided by the library which converts/formats the number to hex directly.
Here's my solution:
public class Solution {
private static final String characters = "0123456789abcdef";
private static final char[] digits = characters.toCharArray();
private Stack<Integer> stack = new Stack<>();
public String toHex(int num) {
if (num == 0) return "0";
stack.clear();
while (num != 0) {
stack.push(getDigit(num));
num = num >>> 4;
System.out.println(num);
if (stack.size() > 8) break;
}
StringBuilder buffer = new StringBuilder();
while (!stack.empty()) {
buffer.append(digits[stack.pop()]);
}
return buffer.toString();
}
private int getDigit(int num) {
int result = num & 0xF;
return result;
}
}
The problem is that this solution works, but it's not very performant--I'm in the bottom 1% of runtimes with this solution. I'm wondering if my use of the Stack Object or the StringBuilder is causing me grief.
It's also totally possible that a ton of submissions are ignoring the restriction and using the Java APIs anyway. :) But I figured I'd post here and learn how I could make this more efficient.



System.out.println()you currently have within the loop. That probably contributes significantly to the execution time.