Strait Forward C++ (i.e. no tricks)


  • 0
    F
    class Solution {
    public:
        string toHex(int num) {
            unsigned int newNum = (int)num;
            string result;
            do{
                result = num2Hex(newNum%16) + result;
                newNum /= 16;
                
            }while(newNum > 0);
            
            return result;
        }
        char num2Hex(unsigned int num){
            char hex[] = {'0', '1', '2', '3','4','5','6','7','8','9','a','b','c','d','e','f'};
            return hex[num];
        }
    };
    

Log in to reply
 

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