Leetcode 405 Solution
This article provides solution to leetcode question 405 (convert-a-number-to-hexadecimal)
Access this page by simply typing in "lcs 405" in your browser address bar if you have bunnylol configured.
Leetcode Question Link
https://leetcode.com/problems/convert-a-number-to-hexadecimal
Solution
class Solution {
public:
string toHex(int num) {
uint unum = (uint)num;
string res;
while (unum)
{
int cur = unum % 16;
if (cur < 10)
res += to_string(cur);
else if (cur == 10)
res += 'a';
else if (cur == 11)
res += 'b';
else if (cur == 12)
res += 'c';
else if (cur == 13)
res += 'd';
else if (cur == 14)
res += 'e';
else if (cur == 15)
res += 'f';
unum /= 16;
}
reverse(res.begin(), res.end());
return res.empty() ? "0" : res;
}
};