C++ solution easy to understand


  • 0
    G
    class Solution {
    public:
        string toHex(int num) 
        {
            vector<string> H = {"0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"};
            
            if(!num)
            {
                return H[0];
            }
            
            string Result;
            
            int i = 0;
            while(num && i < 32)
            {
                Result += H[num & 15];
                num >>= 4;
                i+= 4;
            }
            
            reverse(Result.begin(),Result.end());
            return (Result);
        }
    };

Log in to reply
 

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