C++ solution


  • 0
    A
    class Solution {
    public:
        string toHex(int num) {
            if (!num)
                return "0";
            
            string ans;
            int count = 8;
            while (num && count--) {
                int v = num & 15;
                ans.push_back(v < 10 ? '0' + v : 'a' + v - 10);
                num >>= 4;
            }
            reverse(ans.begin(), ans.end());
            return ans;
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.