Leetcode 405. Convert a Number to Hexadecimal | | Leetcode 405. Convert a Number to Hexadecimal 文章作者:Tyan博客:noahsnail.com | CSDN | 简书 1. Description 2. Solution Version 1 12345678910111213141516class Solution {public: string toHex(int num) { if(num == 0) { return "0"; } string s; const string HEXO = "0123456789abcdef"; while(num != 0 && s.length() < 8) { s += HEXO[num & 0xf]; num >>= 4; } reverse(s.begin(), s.end()); return s; }}; Version 2 123456789101112131415161718class Solution {public: string toHex(int num) { if(num == 0) { return "0"; } unsigned int n = num; string s; const string HEXO = "0123456789abcdef"; while(n) { int remainder = n % 16; s += HEXO[remainder]; n >>= 4; } reverse(s.begin(), s.end()); return s; }}; Reference https://leetcode.com/problems/convert-a-number-to-hexadecimal/description/ 如果有收获,可以请我喝杯咖啡! 赏 微信打赏 支付宝打赏